【问题标题】:Max Consecutive Ones in a given array(Leetcode)给定数组中的最大连续个数(Leetcode)
【发布时间】:2018-09-26 08:40:59
【问题描述】:

我试图在 O(n) 时间复杂度的数组中找到最大连续 1 数,并且不使用超过一个单位的空间。(除了用于遍历数组的变量)。这是我想出的代码,但 Leetcode 提交显示它超过了时间限制。我很难理解它是如何超过时间限制的,因为我只迭代数组的每个元素一次并在同一次迭代中运行逻辑。

public class MaxConsecutive {

  public static void main(String[] args) {

    System.out.println(findMaxConsecutiveOnes(new int[] {0,1,0,0,0,0,1,1,1}));
  }

   public static int findMaxConsecutiveOnes(int[] nums) {

     int i=0, j=0, maxLen=0;

     while(j<nums.length) {

         if(nums[j] == 1) {

            i=j; 
            while (true) {
                while (j < nums.length && nums[j] == 1) {
                    j++;
                }

                if( j - i > maxLen) {
                    maxLen = j - i;
                    j++;
                    break;
                }
            }
         }
         else
             j++; 
     }
     return maxLen;
    }
}

【问题讨论】:

  • 嗯,我可以看到三个while 循环
  • 是的,你为什么需要不止一个?
  • 是的,确实有 3 个 while 循环,但它只遍历数组一次。我同意这不是最好的解决方案,但这是我想到的第一个解决方案。

标签: java arrays algorithm


【解决方案1】:
while (true) {
    // ....

    if( j - i > maxLen) {
        maxLen = j - i;
        j++;
        break;
    }

}

在这部分中,除非当前长度大于maxLen,否则您不会打破无限循环。这个循环可以是无限的,程序会给你 TLE。

我删除了循环并在这里做了一些修改:

public static int findMaxConsecutiveOnes(int[] nums) {      

     int i=0, j=0, maxLen=0;

     while(j<nums.length) {

         if(nums[j] == 1) {

            i=j;
            while (j + 1 < nums.length && nums[j + 1] == 1) {
                j++;
            }

            if( j - i + 1 > maxLen) {
                maxLen = j - i + 1;
            }
         }

         j++;        
     }

     return maxLen;

}

【讨论】:

    【解决方案2】:

    三个while 循环。在我看来,您的代码太复杂了。你只需要一个循环。

    一些更简单的解决方案:

    public static int findMaxConsecutiveOnes(int[] nums) {
        int maxCount = 0;
        int currentCount = 0;
        for (int number : nums) {
            if (number == 1) {
                currentCount++;
            } else {
                currentCount = 0;
            }
            maxCount = Math.max(maxCount, currentCount);
        }
        return maxCount;
    }
    

    【讨论】:

      【解决方案3】:

      一个循环就够了。最低时调用 Math.max。

      public static int findMaxConsecutiveOnes(int[] nums) {
      
       int reset = 0, run=0, maxLen=0;
      
       for (int i = 0 ; i < nums.length; i++) {
      
          if ( nums[i] == 1 ) {
              run++;
          } else {
              maxLen = Math.max(maxLen, run);
              run = 0;
          }
       }
       maxLen = Math.max(maxLen, run);
      
       return maxLen;
      }
      

      【讨论】:

        【解决方案4】:
        public class ConsecutiveOne {
        
        
            public  static int  Consecutiveones(int[] nums)
            {
                int count=0;
                HashSet Consecutivecount=new HashSet();
               // Object[] obj=new Object[]{};
        
                for(int i=0;i<nums.length;i++)
                {
                    if(nums[i]==1)
                    {
                        count++;
        
                        if(i==nums.length-1)
                        {
                            Consecutivecount.add(count);
                        }
                    }
                    else
                    {
        
                        Consecutivecount.add(count);
                        count=0;
                    }
                }
        
        
        
        
        
        return (int) Collections.max(Consecutivecount);
            }
            public static void main(String[] args)
            {
                int[] arr={1,1,1,1,0,0,1,1,1,0,1,1,1,1,1};
        
            int longestCOunt= Consecutiveones(arr);
            System.out.println(longestCOunt);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-28
          • 1970-01-01
          • 2016-10-16
          • 1970-01-01
          • 2021-01-21
          • 2020-01-22
          • 2020-07-11
          • 2021-01-15
          相关资源
          最近更新 更多