congr / world

2 stars 1 forks source link

LeetCode : 485. Max Consecutive Ones #513

Closed congr closed 5 years ago

congr commented 5 years ago

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

congr commented 5 years ago

O(N)

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int cnt = 0, max = 0; 
        for (int n : nums) {
            if (n == 1) cnt++;
            else cnt = 0;
            max = Math.max(max, cnt);
        }

        return max;
    }
}