上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 36 下一页
摘要: 动态规划 class Solution { public int numDecodings(String s) { if (s.charAt(0) == '0'){ return 0; } int n = s.length(); /** * 定义dp[i]为长度为i的子串解码的总数,字符串为空默认也 阅读全文
posted @ 2022-01-21 16:11 振袖秋枫问红叶 阅读(107) 评论(0) 推荐(0)
摘要: 完全背包 import java.util.Arrays; class Solution { public int numSquares(int n) { /** * dp[j]定义为和为j的完全平方数的最小个数 * 因为是求最小值,因此所有位置初始化为最大值 * 初始值dp[0] == 0 */ 阅读全文
posted @ 2022-01-20 16:42 振袖秋枫问红叶 阅读(52) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; /** * dp[i][j]定义为从起点出发到当前这个点的最小路径和 * 初始值dp[0] 阅读全文
posted @ 2022-01-20 15:01 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要: 动态规划 import java.util.List; class Solution { public int minimumTotal(List<List<Integer>> triangle) { /** * 逆向思维 * 从下往上寻找最小路径,路径的条数越来越少,更容易找到 * 假设最后一行下 阅读全文
posted @ 2022-01-20 14:43 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int numTrees(int n) { /** * 定义dp[i]为i个节点可以组成的二叉搜索树的个数 * 0个节点也算一棵树 */ int[] dp = new int[n + 1]; dp[0] = 1; dp[1] = 1; /** 阅读全文
posted @ 2022-01-19 22:53 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int integerBreak(int n) { /** * 定义dp[i]为正整数i拆分结果的最大乘积 * 初始值dp[2] == 1 */ int[] dp = new int[n + 1]; dp[2] = 1; /** * 咋一看, 阅读全文
posted @ 2022-01-19 21:39 振袖秋枫问红叶 阅读(74) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { /** * 定义dp[i][j]为到达该坐标的路径总和 * 第一行和第一列的坐标,只能从左或者上进行访问 * 注意:如果有障碍物,那这一 阅读全文
posted @ 2022-01-19 11:11 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int uniquePaths(int m, int n) { /** * 定义dp[i][j]为到达该坐标的路径总和 * 第一行和第一列的坐标,只能从左或者上进行访问,其值初始化都为1 */ int[][] dp = new int[m][ 阅读全文
posted @ 2022-01-19 10:50 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int minCostClimbingStairs(int[] cost) { /** * 最少有两个台阶,因此不用提前判断dp数组空指针异常的情况 * 索引从0开始 * dp[i]指的是到达第i个台阶并且向上爬所需要的最小费用 */ int 阅读全文
posted @ 2022-01-19 10:18 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int climbStairs(int n) { /** * 因为从第一层台阶开始,初始了dp[1]和dp[2] * 为了避免dp[2]空指针异常,提前判断一下 */ if (n < 2){ return n; } int[] dp = ne 阅读全文
posted @ 2022-01-18 14:21 振袖秋枫问红叶 阅读(58) 评论(0) 推荐(0)
上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 36 下一页