LeetCode 485. Max Consecutive Ones
Given a binary array nums, return the maximum number of consecutive 1's
in the array.
Example 1:
Input: nums = [1,1,0,1,1,1]
Output: 3
method
数组元素是1,cnt
就加1,否则cnt
置零res
记录cnt
的最大值
int findMaxConsecutiveOnes(vector<int>& nums) {
int cnt = 0, res = 0;
for (int i = 0; i < nums.size(); i++) {
cnt = nums[i] ? cnt + 1 : 0;
res = max(res, cnt);
}
return res;
}