有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

给定数组中的java Max连续数(Leetcode)

我试图在时间复杂度为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) 个答案

  1. # 1 楼答案

    一圈就够了。打电话给数学。最大值等于最小值

    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;
    }
    
  2. # 2 楼答案

    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);
        }
    }
    
  3. # 3 楼答案

    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;
    
    }
    
  4. # 4 楼答案

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