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.
class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
       // int count=0;
        int flag=0;
        int sum=0;
        vector<int>count(nums.size());
        for(int i=0;i<nums.size();++i)
        {
            if(nums[i]==1) 
            {
                count[flag]++;
                //count.push_back();
                
            }
            else
            {
                flag=flag+1;
            }
        }
        
        for(int j=0;j<count.size();++j)
        {
            
            if(count[j]>sum) sum=count[j];
        }
        return sum;
    }
};

  

posted @ 2018-07-04 21:04  The_Silencer  阅读(87)  评论(0)    收藏  举报