another Java concise O(n) time O(1) space


  • 0
    W
    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int continuesOne = 0; 
            int count = 0;
            int maxLength = 0; 
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == 1) {
                    count++; 
                    continuesOne++; 
                } else {
                    count = continuesOne+1; 
                    continuesOne = 0; 
                }
                maxLength = Math.max(count, maxLength); 
            }
            return maxLength; 
        }
    }
    

Log in to reply
 

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