Java - 用于查找连续元素的最长序列(逻辑错误)的程序

时间:2016-10-30 16:32:00

标签: java arrays arraylist

我目前正在创建一个程序,用户输入一个整数数组。该程序必须找到最长的连续元素序列。因此,如果用户输入" 3,2,1,2,4,6,7,8,1,2"该程序将输出" 1,2,4,5,6,7,8"。但是,我一直遇到2个错误。

第一个错误是当xs = 1000,97777,487,8274,972837时。该程序将输出" 1000,97777"而不是" 487,8274,972837"。逻辑上这是错误的,因为第一个输出不是" LONGEST"增加连续元素的序列。

第二个错误是当xs = 2,7时。它似乎输出一个空数组而不是" 2,7"。我假设它是因为它可能没有足够的元素吗?

    static int[] increasing(int[] xs){
    ArrayList<Integer> current_array = new ArrayList<Integer>();
    ArrayList<Integer> list = new ArrayList<Integer>();        
    int c_counter = 0;

    for (int i = 0; i<(xs.length); i++){
        if (i==0){
            if (xs[i+1] > xs[i]){
                current_array.add(xs[i]);
                c_counter++; //keeps track of how many elements have been added
            }
        }
        else if ((xs[i] > xs[i-1])){
            if (c_counter==0){
                current_array.add(xs[i-1]); //makes sure the smaller number gets added too
                current_array.add(xs[i]);
                c_counter = c_counter + 2;                    
            } else{
                current_array.add(xs[i]);
                c_counter++;
            }
        } else {
            if (current_array.size()>list.size()){ //compares sizes to find the longest sequence
                list.clear();
                for (int k=0; k<(current_array.size()); k++){
                    if (current_array.get(k) != 0){ //removes any null values
                        list.add(current_array.get(k));
                    }
                }
                current_array.clear(); //clears it to restart and find any longer sequences
                c_counter = 0;                    
            }
        }
    }
    int[] out_array = list.stream().mapToInt(i->i).toArray(); //converts from arraylist to int[] as that's the format it must output
    out_array = list.stream().filter(i->i != null).mapToInt(i->i).toArray();
    return out_array;
}

2 个答案:

答案 0 :(得分:2)

public static int[] increasing(int[] xs){       
    int start = 0;
    int end = 0;
    int temp = 0;       
    for (int i = 0; i < xs.length; i++) {
        if(i==0 || xs[i]<xs[i-1]){
            temp = i;           
        }
        else if(i-temp > end-start){
            start = temp;
            end = i;
        }
    }
 return Arrays.copyOfRange(xs, start, end+1);       
}

答案 1 :(得分:1)

理解代码需要很长时间。试试这个:

$temp_option_arr=array();

如果您想要最长的减少输出,请将 List<Integer> test = new ArrayList<Integer>(); test.add(3); test.add(2); test.add(1); test.add(2); // test.add(4); // test.add(6); // test.add(7); // test.add(8); test.add(1); test.add(2); test.add(1000); test.add(97777); test.add(487); test.add(8274); test.add(972837); List<Integer> output = new ArrayList<Integer>(); List<Integer> temp = new ArrayList<Integer>(); for(int i = 0; i < test.size(); i++) { int current = test.get(i); int next = Integer.MIN_VALUE; if(i + 1 < test.size()) next = test.get(i + 1); if(current > next) { if(output.size() <= temp.size()) { temp.add(current); output = new ArrayList<Integer>(temp); } temp.clear(); } else { temp.add(current); } } output.forEach(i -> System.out.print(i + ", ")); int next = Integer.MAX_VALUE;更改为if(current > next)