Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组的最大递增序列_Java_Arrays - Fatal编程技术网

Java 数组的最大递增序列

Java 数组的最大递增序列,java,arrays,Java,Arrays,有一个数组,其中的元素类似于[1,2,3,4,1,2,3,4,5,1,2,3,4,5,6],所以我需要在这个数组中找到最大的递增序列import java.util.array; import java.util.Arrays; public class Main { public static void main(String atrgs[]) { int[] array = {1,2,3,4,2,4,1,1,2,3,4,5,1,2,3,4,5,6,7,4,3

有一个数组,其中的元素类似于[1,2,3,4,1,2,3,4,5,1,2,3,4,5,6],所以我需要在这个数组中找到最大的递增序列

import java.util.array;
import java.util.Arrays;

public class Main
{
    public static void main(String atrgs[])
    {
        int[] array = {1,2,3,4,2,4,1,1,2,3,4,5,1,2,3,4,5,6,7,4,3,2,1};
        int[] result = biggestIncreasingSeq(array);
        System.out.println(Arrays.toString(result));
    }

    public static int[] biggestIncreasingSeq(int[] array)
    {
        int bestStart = 0;
        int bestEnd = 0;

        int start = 0;
        int end = 0;

        for (int i = 1; i < array.length; ++i)
        {
            // Check if this next element is no longer increasing
            if (array[i] <= array[i - 1])
            {
                // No longer increasing.

                // Update the largest found array, if applicable
                if (end - start > bestEnd - bestStart)
                {
                    // This was a longer sequence
                    bestEnd = end;
                    bestStart = start;
                }

                // Reset for the next sequence
                start = i;
                end = i;
            }
            else
            {
                // Still increasing, update the end
                end = i;
            }
        }

        // Save the sequence to a new array
        int[] result = new int[bestEnd - bestStart + 1];
        for (int i = bestStart; i <= bestEnd; ++i)
        {
            result[i - bestStart] = array[i];
        }

        return result;
    }
}
公共班机 { 公共静态void main(字符串atrgs[]) { int[]数组={1,2,3,4,2,4,1,1,2,3,4,5,1,2,3,4,5,6,7,4,3,2,1}; int[]结果=最大增量序列(数组); System.out.println(Arrays.toString(result)); } 公共静态int[]最大增量seq(int[]数组) { int-bestStart=0; int-bestEnd=0; int start=0; int end=0; 对于(int i=1;i对于(inti=bestStart;i您可以在这里找到解决方案

最长递增子序列


你想做什么do@DanielCentore有一个数组中的元素类似于[1,2,3,4,1,2,3,4,5,1,2,3,4,5,6],所以我需要找到这个数组中最大的递增序列。@SandeepGupta请编辑这个问题,并对您试图实现的目标进行适当的解释。@C-Otto有一个数组中的元素类似于[1,2,3,4,1,2,3,4,5,1,2,3,4,5,6]所以我需要找到这其中最大的递增序列array@SandeepGupta“编辑问题”怎么样?你不明白吗?你应该从链接中包含一些相关信息。链接不是永恒的。