给定数组中的最大连续个数(Leetcode)

时间:2018-09-26 08:40:59

标签: java arrays algorithm

我正在尝试在时间复杂度为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;
    }
}

4 个答案:

答案 0 :(得分:0)

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;

}

答案 1 :(得分:0)

三个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;
}

答案 2 :(得分:0)

一个循环就足够了。至少调用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;
}

答案 3 :(得分:0)

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);
    }
}