0%

max-consecutive-ones

Max Consecutive Ones – LeetCode 485

Problem

Description

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

Example

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.

Answer

Original

Code

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

思路

无话可说。时间复杂度$O(n)$,空间复杂度$O(1)$。
耗时$38$ ms,排名$23.29\%$

Better

思路

还没看到更好的思路。