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
The length of input array is a positive integer and will not exceed 10,000
示例:
问题分析:
求解数组中连续的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);
}
};
Given a binary array, find the maximum number of consecutive 1s in this array.
Note:
The input array will only contain
0and
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);
}
};
相关文章推荐
- 485. Max Consecutive Ones的C++解法
- 485. Max Consecutive Ones
- Leetcode刷题(Max Consecutive Ones)
- leetcode485. Max Consecutive Ones
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones(C语言)
- 485. Max Consecutive Ones*
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones
- leetcode 485. Max Consecutive Ones
- LeetCode 485:Max Consecutive Ones(连续1的最大个数,边界条件)
- 485. Max Consecutive Ones
- Max Consecutive Ones
- 485. Max Consecutive Ones
- Leetcode 485. Max Consecutive Ones
- 485. Max Consecutive Ones(C++)
- LeetCode 485. Max Consecutive Ones(Java)
- LeetCode 485. Max Consecutive Ones
- [LeetCode]485. Max Consecutive Ones(最大连续为1的数量)
- 【LeetCode】485.Max Consecutive Ones_EASY(九)