随笔分类 - LeetCode
摘要:动态规划 class Solution { public int longestPalindromeSubseq(String s) { /** * 类似于《647. 回文子串》 * dp[i][j]定义为区间为[i, j]内最长的回文子串长度 * 初始化每个字符就是一个回文串,即每个dp[i][i
阅读全文
摘要:动态规划 class Solution { public int minDistance(String word1, String word2) { /** * 和《583. 两个字符串的删除操作》一模一样 * 只有一个地方不一样 */ int[][] dp = new int[word1.leng
阅读全文
摘要:动态规划 class Solution { public int minDistance(String word1, String word2) { /** * dp[i][j]定义为以word1[i - 1]结尾的字符串,和以word2[j - 1]结尾的字符串,要想相等需要删除的最小元素个数 *
阅读全文
摘要:动态规划 class Solution { public int numDistinct(String s, String t) { /** * 结合《1143. 最长公共子序列》和《392. 判断子序列》 * dp[i][j]定义为以s[i - 1]结尾的字符串s中出现以t[j - 1]结尾的字符
阅读全文
摘要:完全背包 import java.util.List; class Solution { public boolean wordBreak(String s, List<String> wordDict) { /** * dp[j]定义为长度为j的字符串是否可以由字典中的子串组成,即[0, j -
阅读全文
摘要:动态规划 class Solution { public int countSubstrings(String s) { /** * dp[i][j]定义为区间为[i, j]的子串是否是回文串 */ boolean[][] dp = new boolean[s.length()][s.length(
阅读全文
摘要:动态规划 class Solution { public boolean isSubsequence(String s, String t) { /** * 和《1143. 最长公共子序列》一样 */ int[][] dp = new int[s.length() + 1][t.length() +
阅读全文
摘要:动态规划 class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { /** * 和《1143. 最长公共子序列》一样 */ int[][] dp = new int[nums1.length + 1][nums
阅读全文
摘要:动态规划 class Solution { public int longestCommonSubsequence(String text1, String text2) { /** * dp[i][j]定义为text1[i - 1] == text2[j - 1]时的公共子序列的最大长度 */ i
阅读全文
摘要:动态规划 class Solution { public int findLength(int[] nums1, int[] nums2) { /** * dp[i][j]定义为nums1[i - 1] == nums2[j - 1]时的最长重复子数组的长度 * 因为i - 1必须要大于等于0,所以
阅读全文
摘要:动态规划 import java.util.Arrays; class Solution { public int findLengthOfLCIS(int[] nums) { /** * dp[i]定义为以nums[i]结尾的最长连续递增子序列 * 每个数字自己都可以构成一个序列,因此初始化长度都
阅读全文
摘要:动态规划 import java.util.Arrays; class Solution { public int lengthOfLIS(int[] nums) { /** * dp[i]定义为以nums[i]结尾的最长递增子序列 * 每个数字自己都可以构成一个序列,因此初始化长度都为1 */ i
阅读全文
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { if (prices.length == 1){ return 0; } int[][] dp = new int[prices.length][4]; dp[0][0] = -pr
阅读全文
摘要:动态规划 class Solution { public int maxProfit(int k, int[] prices) { if (prices.length == 0){ return 0; } int[][] dp = new int[prices.length][2 * k + 1];
阅读全文
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { int[][] dp = new int[prices.length][5]; dp[0][0] = 0; dp[0][1] = -prices[0]; dp[0][2] = 0;
阅读全文
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { /** * 因为只能买一次,因此无法确定哪天买了还有每天的持股状态,需要定义一个二维数组分别存储持不持有该股票的利润 * dp[i][0]为第i天持有该股票的利润 * dp[i][1
阅读全文
摘要:贪心 class Solution { int num = 0; public int minCameraCover(TreeNode root) { /** * 如果根节点未被监视,需要单独放一个摄像头 */ if (dfs(root) == 0){ num++; } return num; }
阅读全文
摘要:动态规划 class Solution { public int maxProfit(int[] prices, int fee) { int[][] dp = new int[prices.length][2]; dp[0][0] = -prices[0]; /** * 和《122. 买卖股票的最
阅读全文
摘要:贪心 class Solution { public int monotoneIncreasingDigits(int n) { /** * 将数字拆分为字符数组 * start为第一个需要变为9的位置,后面的位置全部要变为9 */ char[] chars = String.valueOf(n).
阅读全文
摘要:贪心 class Solution { public int wiggleMaxLength(int[] nums) { int prev = 0; int cur = 0; int count = 1; /** * 遍历时记录前一个差值和当前差值,如果差值正负交替,总数就加1,然后更新前一个差值
阅读全文

浙公网安备 33010602011771号