dp求最长递增子序列并输出

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 import java.util.List;
 4 
 5 /**
 6  * Created  on 2016/4/26.
 7  */
 8 public class Testdp {
 9 
10     public static void main(String[] args) {
11         new Testdp().getLIS();
12     }
13 
14     public void getLIS() {
15         List<Integer> nums = null;
16         Integer[] numArray = {1, 4, 8, 1, 2, 3, 5};
17         nums = Arrays.asList(numArray); //输入的数据
18         int len = nums.size();
19 
20         String[] result = new String[len]; // 用来存储输出字符串
21         int dp[] = new int[len];
22 
23         for (int i = 0; i < len; ++i) {
24             dp[i] = 1;
25             result[i] = "" + nums.get(i);
26 
27             for(int j=0; j<i; ++j) {
28                 if (nums.get(j) < nums.get(i) && dp[j] + 1 > dp[i]) {
29                     result[i] = result[j] + " " +  nums.get(i);
30                     dp[i] = dp[j] + 1;
31                 }
32             }
33         }
34 
35         int maxLen = 0;
36         int maxIndex = 0;
37         for(int i=0; i<len; ++i) {
38             if (maxLen < dp[i]) {
39                 maxLen = dp[i];
40                 maxIndex = i;
41             }
42         }
43         System.out.println(result[maxIndex]);
44         System.out.println(maxLen);
45 
46     }
47 
48 }

 

转载于:https://www.cnblogs.com/set-cookie/p/5434427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值