8-line O(N) Java beat 82%


  • 0
    A
    public int findMaxConsecutiveOnes(int[] nums) {
        int prevZeroPos = -1, ret = 0, cur = 0;
        for(int i=0; i<nums.length; i++){
            if(nums[i]==1) cur++; 
            else{cur = i-prevZeroPos; prevZeroPos = i;}
            if(cur>ret) ret = cur;
        }
        return ret;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.