给定一个二进制数组, 计算其中最大连续1的个数。
输入:
[1,1,0,1,1,1]
输出:
3
解释:
开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
0
和1
。class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0;
int sz = nums.size();
int i = 0, j = 0;
while(i < sz){
while(j < sz && nums[j] == 1){
j++;
}
res = max(res, j - i);
j++;
i = j;
}
return res;
}
};
leetcode 485. 最大连续1的个数(Max Consecutive Ones)
原文:https://www.cnblogs.com/zhanzq/p/10593665.html