Max Consecutive Ones
Problem
Given a binary array nums
, return the maximum number of consecutive 1
’s in the array.
Constraints
1 <= nums.length <= 105
nums[i]
is either0
or1
.
Solution
The problem Max Consecutive Ones
can be solved by simply counting the number of consecutive 1’s in the array as told by the problem instruction.
Implementation
class Solution
{
public:
int findMaxConsecutiveOnes(vector<int> &nums)
{
nums.push_back(0);
int ret = 0, count = 0;
for (int num : nums)
{
if (num)
count += 1;
else
{
ret = max(ret, count);
count = 0;
}
}
return ret;
}
};