今天偶遇这个题,具体题目如下,程序是在网上找的,修改了一下, 原贴关了,没找到~~
public static void main(String[] args) {
Integer[] a = {1, 5, 7, 8, 43, 98, 23, 4, 9, 2};
List<Integer> list = new ArrayList<Integer>(Arrays.asList(a));
Collections.sort(list);
System.out.println(list);
System.out.println(increList2(list.toArray(new Integer[0])));
}
private static LinkedList<Integer> increList2(Integer[] a) {
int length = a.length;
LinkedList<Integer> list = new LinkedList<Integer>();
if(length == 0) {
return list;
}
int maxCount = 0;
int index = 0;
int[] dp = new int[length];
for(int i = 0; i < length; i++) {
dp[i] = 1;
for(int j = 0; j < i; j++) {
if((a[j] + 1) == a[i]) {
dp[i] = dp[i] > dp[j] + 1 ? dp[i] : dp[j] + 1;
}
if(maxCount < dp[i]) {
maxCount = dp[i];
// 并记录 最大连续出现时的下标
index = i;
}
}
}
System.out.println(Arrays.toString(dp));
System.out.println(index);
for(int i = 0; i < index; i++) {
if(i >= (index - maxCount)) {
list.addLast(a[i + 1]);
}
}
System.out.println(list);
return list;
}