今天是第五十二天,依旧是动态规划专题
class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; Arrays.fill(dp, 1); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } } } int res = 0; for (int i = 0; i < dp.length; i++) { res = Math.max(res, dp[i]); } return res; } }
子序列的入门题
class Solution { public int findLengthOfLCIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = 1; } int res = 1; for (int i = 0; i < n - 1; i++) { if (nums[i + 1] > nums[i]) { dp[i + 1] = dp[i] + 1; } res = res > dp[i + 1] ? res : dp[i + 1]; } return res; } }
非常的模版
class Solution { public int findLength(int[] nums1, int[] nums2) { int result = 0; int n = nums1.length; int m = nums2.length; int[][] dp = new int[n + 1][m + 1]; for (int i = 1; i < n + 1; i++) { for (int j = 1; j < m + 1; j++) { if (nums1[i - 1] == nums2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; result = Math.max(result, dp[i][j]); } } } return result; } }
没太懂这个二维数组dp,明天再看看
子序列问题有点难
标签:nums,int,res,训练营,随想录,++,length,第五十二,dp From: https://www.cnblogs.com/catSoda/p/16948281.html