Simple Java solution using Q


  • 0
     public int findMaxConsecutiveOnes(int[] nums) {
            Queue<Integer> q = new LinkedList<Integer>();
          int max =0;
          int i =0;
          while(i < nums.length)
          {
        	  while(i < nums.length && nums[i] !=0)
        	  {
        		  q.add(nums[i]);
        		  i++;
        	  }
        	  if(q.contains(0))
        	  {
        		  if(q.size() > max)
        		    max = q.size();
        		  while(!q.isEmpty() && q.peek() != 0)
            		  q.remove();
        		  if(!q.isEmpty() && q.peek() == 0)
        			  q.remove();
        	  }
        	  if(i < nums.length)
        		  q.add(nums[i]);
        	  i++;
          }
          if(!q.isEmpty() && q.size() > max)
        	  max = q.size();
          return max;
        }
    

Log in to reply
 

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