easy java version, modified from max consecutive ones I


  • 0
    S

    Easily understand version, keep the records of the previous consecutive ones and the current consecutive ones, update the max each time.

    public int findMaxConsecutiveOnes(int[] nums) {
    		int pre=-1, cur=0, max=0;
    		for(int i=0;i<nums.length;i++){
    			if(nums[i]==1){
    				cur++;
    			}else{
    				max=Math.max(max, cur+pre+1);
    				pre=cur;
    				cur=0;
    			}
    		}
    		max=Math.max(max, pre+cur+1);
    		return max;
    	}

Log in to reply
 

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