Java solution beat 98%


  • 0
    public class Solution {
        /*
        find():
            int current, max
            for each num : nums:
                if num != 1:
                    max = max(max, current)
                    current = 0
                else: current += 1
            return max(max, current)
        */
        // 1,1,0,1,1,1
        public int findMaxConsecutiveOnes(int[] nums) {
            int current = 0, max = 0;
            for (int num : nums) {
                if (num == 1) current += 1;
                else {
                    max = Math.max(max, current);
                    current = 0;
                }
            }
            return Math.max(max, current);
    
    }
    

    }


Log in to reply
 

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