Java very simple solution o(n)


  • 0
    W

    ''

    public int findMaxConsecutiveOnes(int[] nums) {
        
        List<Integer> list = new ArrayList<Integer>();
    	int count = 0, length = nums.length -1;
    
    	for(int i = 0; i < nums.length; i++) {
    	
    		if(nums[i] == 1 && i < length) {
    			count++;
    		}
    		else if(nums.length == 1 && nums[i] == 1) {
    			count = 1;
    			list.add(count);
    			count = 0;
    			
    		}
    		else if(nums[i] == 0 && count > 0){
    			list.add(count);
    			count = 0;
    		}
    		else if(nums[i] == nums[length] && nums[i] == 1) {
    			count++;
    			list.add(count);
    			count = 0;
    		}
    		else {
    			count = 0;
    		}
    		
    	}
    	
    	if(list.size() == 0) {
    		return 0;
    	}
    	else {
    		return Collections.max(list);
    		
    	}
        
    }

Log in to reply
 

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