题解 | #最长上升子序列(一)#
最长上升子序列(一)
http://www.nowcoder.com/practice/5164f38b67f846fb8699e9352695cd2f
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定数组的最长严格上升子序列的长度。
* @param arr int整型一维数组 给定的数组
* @return int整型
*/
public int LIS (int[] arr) {
// write code here
int len=arr.length;
if(len==0||arr==null) return 0;
int[] dp=new int[len];
dp[0]=1;
int max=1;
for(int i=1;i<len;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(arr[j]<=arr[i] && dp[i]<=dp[j]) dp[i]=dp[j]+1;
}
max=Math.max(dp[i],max);
}
return max==1? -1:max;
}
}
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 给定数组的最长严格上升子序列的长度。
* @param arr int整型一维数组 给定的数组
* @return int整型
*/
public int LIS (int[] arr) {
// write code here
int len=arr.length;
if(len==0||arr==null) return 0;
int[] dp=new int[len];
dp[0]=1;
int max=1;
for(int i=1;i<len;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(arr[j]<=arr[i] && dp[i]<=dp[j]) dp[i]=dp[j]+1;
}
max=Math.max(dp[i],max);
}
return max==1? -1:max;
}
}