有 Java 编程相关的问题?

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

如何打印int[]数组中最长的数字序列(Java)

我是编程新手,需要打印int[]数组中最长的数字序列。 例如,如果我们有:

int[] numbers = {1, 3, 3, 5, 5, 5, 5, 5, 5, 6, 0, 12, 2, 2, 2, 12, 0};

结果应该是:

String result = "5, 5, 5, 5, 5, 5";

我写了一些不好的代码,但可能会给你一些想法

public String findLargestSequence(int[] numbers) {
        int bestStart = 0;
        int curStart = 0;
        int bestLength = 1;
        int curLength = 1;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > numbers[i - 1]) {
                curLength++;
                if (curLength > bestLength) {
                    bestStart = curStart;
                    bestLength = curLength;
                }
            } else {
                curStart = i;
                curLength = 1;
            }
        }
        List<String> identical = new ArrayList<>();
        for (int i = 0; i < bestLength; i++) {
            identical.add(String.valueOf(numbers[bestStart + i]));
        }
        return Joiner.on(", ").join(identical);
    }

更新 感谢@phatfingers,我发现了问题: (numbers[i] > numbers[i - 1])应该是(numbers[i] == numbers[i - 1])。 但仍然存在另一个问题。 如果我们有类似于:

int[] numbers = {1, 2, 3, 3, 4, 4};

其结果是:

"3, 3"

我认为在这种情况下,我们可以:

1)比如说,我们没有任何一个最长的序列或

2)显示所有序列,如:

String result = "Founded sequences: " + sequence1 + ", " + sequence2;

3)不要使用上述代码

你会怎么做


共 (2) 个答案

  1. # 1 楼答案

    您必须在算法中处理4种情况,可以将其分为两部分:

    设置当前系列的状态:

    • 如果增加,则增加当前系列
    • 当它改变时,重新调整当前的系列赛

    设置最高系列的状态:

    • 如果增加,则增加最大系列
    • 当它改变时,重新调整max serie

    在实际代码中,这些条件在循环中不受尊重

    我评论了两个逻辑错误来说明这个问题:

      if (numbers[i] > numbers[i - 1]) {
        // error : you don't increment the current serie when it grows
        // because the condition if true only if the the current value is 
        // superior to the previous one
        curLength++;
        if (curLength > bestLength) {
            bestStart = curStart;
            bestLength = curLength;
        }
      } 
       // error : you don't reinit the current serie only when it changes
       // because numbers[i] <= numbers[i - 1] is not true if the new number is   
       // superior to the previous one while it is a change
       else {     
        curStart = i;
        curLength = 1;
       }
     }
    

    以下是处理4种情况的建议代码:

    public static String findLargestSequence(int[] numbers) {
    
        // variables to maintain the max serie found and the number associated
        int sizeMaxSerieFound = 0;
        int numberMaxFound = numbers[0];
    
        // variables to maintain the current serie found and the number
        // associated
        int sizeMaxCurrentSerie = 0;
        int numberCurrentSerie = numbers[0];
    
        boolean isMaxSerieIsTheCurrent = true;
    
        for (int i = 0; i < numbers.length; i++) {
            int currentNumber = numbers[i];
    
            // FIRST STEP : set the state of the current serie
    
            // increment the current serie if it grows or for the first
            // iteration
            if (currentNumber == numberCurrentSerie) {
                sizeMaxCurrentSerie++;
            }
            // else we reinit to 1 the current serie
            else {
                sizeMaxCurrentSerie = 1;
                numberCurrentSerie = currentNumber;
                isMaxSerieIsTheCurrent = false;
            }
    
            // SECOND STEP : set the state of the max serie
    
            // we increment the current number of the actual max serie
            if (currentNumber == numberMaxFound && isMaxSerieIsTheCurrent) {
                sizeMaxSerieFound++;
            }
    
            // we reinit the serie because we have found a new greater serie
            else if (currentNumber != numberMaxFound && sizeMaxCurrentSerie > sizeMaxSerieFound) {
                sizeMaxSerieFound = sizeMaxCurrentSerie;
                numberMaxFound = currentNumber;
                isMaxSerieIsTheCurrent = true;
            }
    
        }
    
        List<String> identical = new ArrayList<>();
        for (int i = 0; i < sizeMaxSerieFound; i++) {
            identical.add(String.valueOf(numberMaxFound));
        }
        return Joiner.on(", ").join(identical);
    }
    
  2. # 2 楼答案

    这将显示最大发生率,您还可以计算并打印它们

    public static int consecutive(int[] array) {
            if (array.length <= 1) {
                return array.length;
            }    
            int maxRun = 0;
            for (int i = 1; i < array.length; i++) {
                int thisRun = 1;
                while (i < array.length && array[i - 1] + 1 == array[i]) {
                    thisRun++;
                    i++;
                }
                if (maxRun < thisRun) { // checking geater occurance
                    maxRun = thisRun;
                }
            }
            return maxRun;
        }