485. Max Consecutive Ones

Description:

https://leetcode.com/problems/max-consecutive-ones/description/

Code:

 

Time & Space:
O(n) & O(1)

One thought on “485. Max Consecutive Ones”

  1. Refactored:

    class Solution {
    public:
    int findMaxConsecutiveOnes(vector& nums) {
    int ans = 0, cur = 0;
    for (const auto x : nums) {
    if (x == 1) {
    if (++cur > ans) ans = cur;
    } else {
    cur = 0;
    }
    }
    return ans;
    }
    };

Leave a Reply

Your email address will not be published. Required fields are marked *