Leetcode:485. Max Consecutive Ones

问题描述:485. Max Consecutive Ones

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.

Note:

The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000

难度等级:简单

这个题目很简单是遍历对应的向量,找到1连续最多的个数,返回对应的个数。

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        if(nums.size()<1)return 0;
        int max=0;
        int count=0; 
        for(int i = 0; i<nums.size();i++)
        {
            if(nums[i] == 1 )
            {
                count++;
                if(count>max)
                    max=count;
            }
            else{
                if(count>max)
                    max = count;
                count = 0;          
            }
        }
        return max;
    }
};

先从简单的刷起。。。O(∩_∩)O哈哈~

猜你喜欢

转载自blog.csdn.net/felaim/article/details/80469593