【问题标题】:finding longest sequence of consecutive numbers找到最长的连续数字序列
【发布时间】:2014-12-03 23:01:12
【问题描述】:

问题 H(最长自然后继):

如果第二个整数是自然数序列中第一个整数的后继(1 和 2 是自然后继),则两个连续整数是自然后继。编写一个程序,读取一个数字 N 后跟 N 个整数,然后打印最长的连续自然后继序列的长度。

例子:

输入 7 2 3 5 6 7 9 10 输出 3 这是我目前的代码,我不知道为什么它不起作用

import java.util.Scanner;

public class Conse {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int[] array = new int[x];
        for (int i = 0; i < array.length; i++) {
            array[i] = scan.nextInt();
        }
        System.out.println(array(array));

    }

    public static int array(int[] array) {
        int count = 0, temp = 0;
        for (int i = 0; i < array.length; i++) {
            count = 0;
            for (int j = i, k = i + 1; j < array.length - 1; j++, k++) {
                if (Math.abs(array[j] - array[k]) == 1) {
                    count++;
                } else {
                    if (temp <= count) {
                        temp = count;
                    }
                    break;
                }
            }
        }
        return temp + 1;
    }

}

【问题讨论】:

  • 5 6 7 = 3?不是正确答案?
  • @KevH 是的,这个答案是正确的,但我的程序没有返回它
  • 通过按 然后按 使用 Netbeans 重新格式化代码。

标签: java arrays


【解决方案1】:

为什么是两个循环?怎么样

public static int array(final int[] array) {
    int lastNo = -100;
    int maxConsecutiveNumbers = 0;
    int currentConsecutiveNumbers = 0;

    for (int i = 0; i < array.length; i++) {
        if (array[i] == lastNo + 1) {
            currentConsecutiveNumbers++;
            maxConsecutiveNumbers = Math.max(maxConsecutiveNumbers,
                    currentConsecutiveNumbers);
        } else {
            currentConsecutiveNumbers = 1;
        }
        lastNo = array[i];
    }
    return Math.max(maxConsecutiveNumbers, currentConsecutiveNumbers);
}

【讨论】:

  • 你不需要最后的Math.max
  • 我一开始也是这么想的——但对于 N=1 的情况我需要它。
【解决方案2】:

这似乎有效:

public static int longestConsecutive(int[] array) {
    int longest = 0;
    // For each possible start
    for (int i = 0; i < array.length; i++) {
        // Count consecutive.
        for (int j = i + 1; j < array.length; j++) {
            // This one consecutive to last?
            if (Math.abs(array[j] - array[j - 1]) == 1) {
                // Is it longer?
                if (j - i > longest) {
                    // Yup! Remember it.
                    longest = j - i;
                }

            } else {
                // Start again.
                break;
            }
        }
    }
    return longest + 1;
}

public void test() {
    int[] a = new int[]{7, 2, 3, 5, 6, 7, 9, 10};
    System.out.println("Longest: " + Arrays.toString(a) + "=" + longestConsecutive(a));
}

打印

Longest: [7, 2, 3, 5, 6, 7, 9, 10]=3

【讨论】:

    【解决方案3】:

    由于您的问题与“问题 H”相关,我假设您只是在学习。越简单越好,因此在开始特定道路之前,通常需要将其分解为“what 必须完成”,通过编写解决问题的代码来解决“how 这个可以吗。”

    在这种情况下,您可能会使数组变得过于复杂。如果一个数字比前一个数字大一个,那么它就是一个自然后继者。如果这是真的,增加当前序列的计数。如果没有,我们将开始一个新的序列。如果当前序列长度大于我们看到的最大序列长度,则将最大序列长度设置为当前序列长度。不需要数组 - 您只需要比较两个数字(当前和最后读取的数字)。

    例如:

    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in); 
    
     int N = scan.nextInt();
    
     int maxSequenceLen = 0;  // longest sequence ever
     int curSequenceLen = 0;  // when starting new sequence, reset to 1 (count the reset #)
     int last = 0;
    
     for(int i = 0; i < N; i++) {
         int cur = scan.nextInt();
         if ((last+1) == cur){
             ++curSequenceLen;
         }
         else{
             curSequenceLen = 1;
         }
         if (curSequenceLen > maxSequenceLen){
             maxSequenceLen = curSequenceLen;
         }
         last = cur;
     }
     System.out.println(maxSequenceLen);
    

    警告:我在没有我的 Java 开发环境的计算机上回答这个问题,因此代码未经测试。

    【讨论】:

      【解决方案4】:

      我不确定我是否正确理解了这个问题。这里写的答案假设自然后继者是连续出现的。但如果这不一样,那么这里的解决方案可能不会给出正确的答案。

      假设输入是[7 2 6 3 7 5 6 9 10] 而不是[7 2 3 5 6 7 9 10],那么答案变为2,而自然后继[5 6 7] 存在于数组中。

      如果输入未排序,我们将不得不使用不同的方法。喜欢使用HashSet

      1. 将整个数组加载到 HashSet 中,这会删除重复项。
      2. HashSet 中选择第一个值并将其分配给startend 并将其从集合中移除。
      3. 现在递减 start 并检查它是否存在于 HashSet 中并继续直到 start 的特定值不存在于 HashSet 中,同时从集合中删除正在搜索的值。
      4. end 执行相同操作,但每次迭代都必须增加end 的值。
      5. 我们现在必须在集合中存在从startend 的连续范围,其范围是current_Max = end - start + 1
      6. 在每次迭代中,我们都会跟踪这个current_Max,以得出整个数组的最长自然后继。

      由于HashSet 支持在O(1) 时间添加、删除、更新。该算法将在O(n) 时间内运行,其中n 是输入数组的长度。 可以在 C# 中找到这种方法的代码 here

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多