Simple easy JAVA O(n) solution


  • 0
    V
    public int findMaxConsecutiveOnes(int[] nums) {
            int cur = 0; int max = 0;
            for(int i =0; i < nums.length; i++)
            {
                 if(nums[i] == 1)
                     cur = cur + 1;
                 else cur = 0;
                 if(cur > max)
                    max = cur;
            }
            return max;
        }
    

  • 0
    E
        public int findMaxConsecutiveOnes(int[] nums) {
            int currentMax = 0, count = 0;
            for(int i : nums){
               count = i == 1 ? count + 1 : 0;
               currentMax = count > currentMax ? count : currentMax;
            }
           return currentMax; 
         }
    }

Log in to reply
 

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