https://leetcode.com/problems/longest-increasing-subsequence/description/
解题思路:
- 两个循环来解决:计算当前值 num[i] 被包括在最大增长子序列的情况下dp[i][j]的最大值
代码如下:
class Solution {
public int lengthOfLIS(int[] nums) {
if(nums == null || nums.length == 0) return 0;
int len = nums.length;
int[] dp = new int[len];
Arrays.fill(dp, 1);
int max = 1;
for(int i = 1; i < len; i++){
for(int j = i - 1; j >= 0; j--){
if(nums[i] > nums[j])
dp[i] = Math.max(dp[i], dp[j] + 1);
}
max = Math.max(dp[i], max);
}
return max;
}
}
网友评论