在列表中查找最长的等效值序列?

时间:2014-03-05 23:07:56

标签: java algorithm

我编写了一个代码,用于找出具有相同序列整数的最长子序列。用户输入一个以0结尾的整数序列,代码完成其余部分,代码仅适用于小序列,我无法看到我出错的地方。这是我的代码:

import java.util.*;

public class test1 {

    private static List<Integer> list = new ArrayList<>();
    private static int input;
    private static int counter = 1;

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a series of numbers ending with 0: ");

        boolean itsOk = true;
        while (itsOk) {
            input = scan.nextInt();
            list.add(input);

            if (input == 0)
                itsOk = false;
        }

        int index = 0;
        for (int i = 1; i < list.size(); i++)
            if (list.get(i).equals(list.get(i - 1))) {
                counter++;
                index = i - 2;
            }

        System.out.println("The longest same number sequence starts at index "
                + index + " with " + counter + " values of " + list.get(index));

        Collections.sort(list);
        System.out.println("\tThe sorted series of numbers is : " + list);

    }
}

2 个答案:

答案 0 :(得分:0)

您不能在此处使用固定值:

    int index = 0;
    for (int i = 1; i < list.size(); i++)
        if (list.get(i).equals(list.get(i - 1))) {
            counter++;
            index = i - 2;
        }

index = i-2仅在序列长度恰好为3个元素时才有效。

相反,您应该创建一个变量来存储当前值之前最近看到的值,并将当前值与值进行比较。此外,您应该创建一个变量来存储当前序列的长度,以及当前序列之前已经看到的最大序列的长度。如果当前序列长度超过先前的序列长度,则仅更新'index'。

答案 1 :(得分:0)

您不会跟踪您的子序列所包含的数字,也不会记录序列开头的索引或长度。您需要在单独的变量中跟踪这些。

正如我在评论中所说,index = i - 2可以产生index == -1,所以这也是正确的。

此外,即使纠正了这些语义错误,您的答案也会在技术上不正确,因为您在列表中包含了终止0,所以请确保不要包含它。

private static List<Integer> list = new ArrayList<>();
private static int input;
private static int counter = 1;

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a series of numbers ending with 0: ");

    while (true) {
        input = scan.nextInt();
        if (input == 0) {
            break;  // don't add 0 to the list
        }
        list.add(input);
    }

    int index = 0;              // the current beginning of the sequence
    int currNum;                // the current candidate for the sequence num
    int theNum = list.get(0);   // keep track of the num with the longest sequence
    int theCount = 0;           // keep track of the count (length) of the longest sequence
    int theIndex = 0;           // keep track of where the sequence began
    for (int i = 1; i < list.size(); i++) {
        currNum = list.get(i-1);
        if (list.get(i).equals(currNum)) {
            if (counter == 1) {
                // note that a sequence is beginning, and its location
                index = i - 1;
            }
            counter++;
            if (counter > theCount) {
                // check if you've found a longer sequence
                theCount = counter;
                theNum = currNum;
                theIndex = index;
            }
        } else {
            // sequence broken, count from scratch
            counter = 1;
        }
    }

    System.out.println("The longest same number sequence starts at index "
            + theIndex + " with " + theCount + " values of " + theNum);

    Collections.sort(list);
    System.out.println("\tThe sorted series of numbers is : " + list);

}

请参阅ideone上运行的代码。