Max Consecutive Ones
Problem
Given a binary array nums
, return the maximum number of consecutive 1
's in the array.
Solution Approach
Click - to see solution code
- C++
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int ans = 0, cnt = 0;
int n = nums.size();
for (int i = 0; i < n; i++) {
if (nums[i])
cnt++;
else {
ans = max(ans, cnt);
cnt = 0;
}
}
ans = max(ans, cnt);
return ans;
}
};