Java 数组的最大递增序列
有一个数组,其中的元素类似于[1,2,3,4,1,2,3,4,5,1,2,3,4,5,6],所以我需要在这个数组中找到最大的递增序列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
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“编辑问题”怎么样?你不明白吗?你应该从链接中包含一些相关信息。链接不是永恒的。