java查找最长的数组序列

时间:2016-12-07 19:29:22

标签: java arrays arraylist

让我们说我有一个阵列。 {} 3,2,5,7,10,1,5,7,11,15。 我想找到这个阵列中增长最快的系列。 例如{2,5,7,10}和{1,5,7,11,15}是这个数组的增加系列,但第二个更长。

2 个答案:

答案 0 :(得分:0)

这样做很有趣,因此我会展示它

    int[] allNumbers = {3,2,5,7,10,1,5,7,11,15,1,2,3};
    ArrayList<Integer> series = new ArrayList<>();
    ArrayList<Integer> currentLongestSeries = new ArrayList<>();

    for(int index = 0; index < allNumbers.length; index++)
    {
        System.out.println("Checking " + allNumbers[index] + "\nCurrent series is " + Arrays.toString(series.toArray()));
        //starting condition
        if(series.size() == 0)
        {
            series.add(allNumbers[index]);
        }
        //adding to our series
        else if(series.get(series.size() - 1) < allNumbers[index])
        {
            series.add(allNumbers[index]);
        }
        //check how big our list is
        else
        {
            if(series.size() > currentLongestSeries.size())
            {
                currentLongestSeries = series;
                series = new ArrayList<>();
                series.add(allNumbers[index]);
            }
            else
            {
                series = new ArrayList<>();
                series.add(allNumbers[index]);
            }
            System.out.println("Current biggest series is " + Arrays.toString(currentLongestSeries.toArray()));
        }
    }
    //now lets check one last time because we still have a series to check
    if(series.size() > currentLongestSeries.size())
    {
        currentLongestSeries = series;
        System.out.println("Current biggest series is " + Arrays.toString(currentLongestSeries.toArray()));
    }
    else
    {
        System.out.println("Last series is " + Arrays.toString(series.toArray()));
    }
    System.out.println("FINAL SERIES is " + Arrays.toString(currentLongestSeries.toArray()));

答案 1 :(得分:0)

这个作业的解决方案是迭代数组,创建一个元素列表,在你去的时候提升价值。

点击不大的值后,请使用之前的列表检查此列表:如果更长,请替换上一个列表。

有很多方法可以完成这项任务。这是我的版本:

import java.util.ArrayList;
import java.util.List;

public class TheLongestSequence {

    public static void main(String[] args) {
        int[] myArray = new int[]{3,2,5,7,10,1,5,7,11,15};

        // Prime the pump with the first element
        int previous = myArray[0];
        List<Integer> longestArray = new ArrayList<Integer>();
        List<Integer> currentArray = new ArrayList<Integer>();
        currentArray.add(previous);

        // Cycle through the array
        for (int i = 1; i < myArray.length; i++) {
            if (myArray[i] > previous) {
                currentArray.add(myArray[i]);
            } else {
                previous = myArray[i];
                // Check if this array is larger than the previous one
                if (currentArray.size() > longestArray.size()) {
                    longestArray = currentArray;
                }

                currentArray = new ArrayList<Integer>();
                currentArray.add(myArray[i]);
            }
        }

        // Double check the last array isn't the longest one
        if (currentArray.size() > longestArray.size()) {
            longestArray = currentArray;
        }

        System.out.println(longestArray);
    }
}

输出:

[1, 5, 7, 11, 15]