【数组】Leetcode编程题解:485. Max Consecutive Ones Add to List

来源:互联网 发布:淘宝有没有死店一说 编辑:程序博客网 时间:2024/06/05 02:29

题目:
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;
}
};

0 0
原创粉丝点击