485. Max Consecutive Ones

Posted lvbbg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了485. Max Consecutive Ones相关的知识,希望对你有一定的参考价值。

 

485. Max Consecutive Ones


 

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

Example 1:

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

 

Solution1:

 1 class Solution {
 2 public:
 3     int findMaxConsecutiveOnes(vector<int>& nums) {
 4         int a=0;
 5         int b=0;
 6         for (int i = 0; i < nums.size(); i++)
 7             if (nums[i] == 1){
 8                 b++; 
 9             }
10             else{
11                 a = max(a,b);
12                 b=0;
13             }
14         a = max(a, b);
15         return a;
16     }
17 };

 

以上是关于485. Max Consecutive Ones的主要内容,如果未能解决你的问题,请参考以下文章