题目:
Given a binary array, find the maximum number of consecutive 1s in this array.
样例:
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 i.
这道题没什么难度,就是简单的统计1的数量,然后进行保存,遇到0就重新统计,将这一次的数量与上一次进行比较,保存多的数量。
提交代码:
class Solution {
public:
int findMaxConsecutiveOnes(vector& nums) {
int len = nums.size(), result = 0, max = 0;
for(int i = 0; i < len; i++) {
if(nums[i] == 1) {
result += 1;
if(result > max)
max = result;
}
if(nums[i] == 0)
result = 0;
}
return max;
}
};
【数组】Leetcode编程题解:485. Max Consecutive Ones Add to List
最新推荐文章于 2021-05-27 10:09:53 发布