每日算法-05(最长上升子序列)
求最长上升子序列
第一行输入一个数n,接下来一行,输入n个数值,求最长上升子序列;
输入例子:
5
2 1 8 3 6
输出:
3
解析:1 3 6
赶脚这个代码还是很菜,先上菜吧,开个胃,哈哈哈;
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int n=in.nextInt(); int i=0,j=0; int dp[]=new int[n+1];int aa[]=new int [n+1]; for(i=0;i<n;i++) { aa[i]=in.nextInt(); } int ans=0; for(i=0;i<aa.length;i++) { dp[i]=1; for(j=0;j<i;j++) { if(aa[j]<aa[i]) { dp[i]=Math.max(dp[j]+1,dp[i]); } } ans=Math.max(ans,dp[i]); } System.out.print(ans); return; } }
仅自己这样想的,不包含科学质上对哈,有错欢迎提出。?
相关推荐
湾区人工智能 2020-11-20
Pokemogo 2020-11-16
baijingjing 2020-11-16
baijingjing 2020-11-15
Site 2020-11-07
lwnylslwnyls 2020-11-06
justaipanda 2020-11-05
MachineIntellect 2020-11-02
xueyuediana 2020-10-30
GeraldJones 2020-10-30
Tips 2020-10-29
baijingjing 2020-10-28
baijingjing 2020-10-27
硕鼠 2020-10-26
playoffs 2020-10-26
scuyxi 2020-10-25
playoffs 2020-10-25
yise001 2020-10-23