不重复的整数数组,取最长连续递增子序列

今天偶遇这个题,具体题目如下,程序是在网上找的,修改了一下, 原贴关了,没找到~~

 不重复的整数数组,取最长连续递增子序列_第1张图片

public static void main(String[] args) {
        Integer[] a = {1, 5, 7, 8, 43, 98, 23, 4, 9, 2};
        List list = new ArrayList(Arrays.asList(a));
        Collections.sort(list);
        System.out.println(list);
        System.out.println(increList2(list.toArray(new Integer[0])));

    }

    private static LinkedList increList2(Integer[] a) {
        int length = a.length;
        LinkedList list = new LinkedList();
        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;
    }

 

你可能感兴趣的:(java面试算法题)