Max Consecutive ones


  • 0
    _

    O(n) simple java solution.(self explanatory)

    class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int c=0,max=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i]==1) c++;
                if(nums[i]==0) {
                    if(c>max)  max=c;
                    c=0;
                }
            }
            if(c>max)  max=c;
            return max;
        }
    }
    

Log in to reply
 

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