485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.


求连续1的最大值

C++(36ms):
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int ans = 0 ;
        int num = 0 ;
        for (int i : nums){
            if (i == 1)
                num++ ;
            else
                num = 0 ;
            ans = max(ans,num) ;
        }
        return ans ;
    }
};

 

posted @ 2017-07-04 13:50  __Meng  阅读(124)  评论(0编辑  收藏  举报