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. Note:
给出一个二进制数组,在数组中找到连续的1最大的长度。 例1: 输入:[1,1,0,1,1,1] 输出:3 解释:开头两个数字和最后三个数字是连续的1。 最大的连续的1是3个。 注意:
无非就是遍历数组,检查连续的1,用一个临时变量记录每次连续的1的个数,连续结束时判断是否比最大的连续个数要大。
代码的写法可以有很多种,也会随着写法不同带来一些效率差异,但时间复杂度是一样的。
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0;
int temp = 0;
bool flag = true;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 1) {
if (flag) temp++;
else {
temp = 1;
flag = true;
}
if (temp > res) res = temp;
} else
if (flag) flag = false;
}
return res;
}
};