提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
每次当初始化的时候,都要回顾一下dp[i][j]的定义,不要凭感觉初始化。
class Solution {
public int maxUncrossedLines(int[] nums1, int[] nums2) {
int m = nums1.length, n = nums2.length;
int[][] dp = new int[m+1][n+1];
for(int i = 1; i <= m; i ++){
for(int j = 1; j <= n; j ++){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[m][n];
}
}
在这里插入代码片class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
if(n == 1){
return nums[0];
}
int[] dp = new int[n+1];
int res = Integer.MIN_VALUE;
for(int i = 1; i <= n; i ++){
dp[i] = Math.max(dp[i-1]+nums[i-1],nums[i-1]);
res = Math.max(res,dp[i]);
}
return res;
}
}
class Solution {
public boolean isSubsequence(String s, String t) {
int m = s.length(), n = t.length();
int[][] dp = new int[m+1][n+1];
for(int i = 1; i <= m; i ++){
for(int j = 1; j <= n; j ++){
if(s.charAt(i-1) == t.charAt(j-1)){
dp[i][j] = dp[i-1][j-1]+1;
}else{
dp[i][j] = dp[i][j-1];
}
}
}
return dp[m][n] == m;
}
}