Easy Java Solution


  • 15

    This is a really easy problem. No explanation :)

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

  • 0
    D
    This post is deleted!

  • 1
    H

    I did a similar solution. But updated maxSum only when nums[i] is a 0

    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            if(nums==null||nums.length==0) return Integer.MIN_VALUE;
            int maxSum=0;int curSum=0;
            for(int i=0;i<nums.length+1; i++){
                if(i<nums.length&&nums[i]==1){curSum++;}
                else{ maxSum = Math.max(curSum,maxSum);
                      curSum = 0;}
            }
        return maxSum;
        }
    }
    

  • 0
    W

    @hihihahahoho I believe this is faster and a better solution, since it has to update and check max every time


  • 0
    T

    @hihihahahoho nope,the output of your code is 2 when the input num[]={1,1,0,1,1,1};


  • 0
    T

    @wolfadam14 we have to check maxSum every time cause if we don't,the last consecutive ones can't be counted.Here is an example:
    nums[1 1 1 0 1 1 1]
    when i=6;we don't update maxSum here.


  • 0

    Thanks for sharing! Here is the solution in "opposite" way, update max only on 0 (and cnt > 0), which is similar to many other problems such as Count and Say, Missing Range, Summary Ranges, Encode String...

        public int findMaxConsecutiveOnes(int[] nums) {
            int max = 0, cnt = 0, n = nums.length;
            for (int i = 0; i <= n; i++) {
                if (i == n || nums[i] == 0) {
                    if (cnt > 0) {
                        max = Math.max(max, cnt);
                        cnt = 0;
                    }
                } else {
                    cnt++;
                }
            }
            return max;
        }
    

  • 0
    L

    @hihihahahoho
    I think you can return Math.max(curSum,maxSum), so you don't have to check i<nums.length every time.


  • 0
    public int findMaxConsecutiveOnes(int[] nums) {
        int ones = 0, maxOnes = 0;
        for (int num : nums)
            if (num != 1)   ones = 0;
            else            maxOnes = Math.max(maxOnes, ++ones);
        return maxOnes;
    }
    

  • 0

    Sliding window solution

    public int findMaxConsecutiveOnes(int[] nums) {
            if(nums == null || nums.length == 0) return 0;
            int res = 0;
            for(int left = 0; left < nums.length; left ++){
                if(nums[left] == 0) continue;
                int right = left;
                while(right + 1 < nums.length && nums[right + 1] == 1){
                    right ++;
                }
                res = Math.max(res, right - left + 1);
            }
            return res;
        }
    

Log in to reply
 

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