题4:最长连续递增子序列(部分有序)
1 package recursion; 2 3 import java.util.Arrays; 4 5 /** 6 * @author zsh 7 * @company wlgzs 8 * @create 2019-02-18 15:35 9 * @Describe 题4·最长连续递增子序列(部分有序) 10 * (1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8) 11 */ 12 public class Main7 { 13 14 /** 15 * 16 * @param arr 待查找数组 17 * @return 最长的递增子序列 18 */ 19 static int[] subsequence(int[] arr){ 20 int[] sub = new int[arr.length]; 21 int begin = 0; 22 int end = 0; 23 int count = 0; 24 for (int i = 1; i < arr.length; i++) { 25 if (arr[i] > arr[end]){ 26 end ++; 27 }else { 28 if ((end - begin) > count){ 29 int l = end - begin; 30 sub = new int[l+1]; 31 for (int j = 0; j <= l ; j++) { 32 sub[j] = arr[begin]; 33 begin ++ ; 34 } 35 } 36 begin = i; 37 end = i; 38 } 39 if (i == arr.length -1){ 40 if ((end - begin) > count){ 41 int l = end - begin; 42 sub = new int[l+1]; 43 for (int j = 0; j <= l ; j++) { 44 sub[j] = arr[begin]; 45 begin ++ ; 46 } 47 } 48 } 49 } 50 return sub; 51 } 52 53 public static void main(String[] args) { 54 int[] sub = new int[]{1,9,2,5,7,3,4,6,8,0,1,2,3,4,5,6,8}; 55 System.out.println(Arrays.toString(subsequence(sub))); 56 } 57 }