您的位置:首页 > 其它

Max Consecutive Ones问题及解法

2017-04-28 13:56 134 查看
问题描述:

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

Note:

The input array will only contain 
0
 and 
1
.
The length of input array is a positive integer and will not exceed 10,000

示例:

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.

问题分析:

求解数组中连续的1的个数的最大值,过程详见代码:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0;
int m = res;
for(int i = 0;i < nums.size(); i++)
{
if(nums[i] == 0)
{
m = max(m,res);
res = 0;
}
else res++;
}
return max(res,m);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: