[LeetCode] Max Consecutive Ones

485. Max Consecutive Ones

Given a binary array nums, return the maximum number of consecutive 1‘s in the array.

1
2
3
4
5
6
7
8
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0, now = 0;
for(auto& x : nums) res = max(res, now = x ? now + 1 : 0);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/02/03/PS/LeetCode/max-consecutive-ones/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.