01 2022 档案

摘要:完全背包 import java.util.Arrays; class Solution { public int coinChange(int[] coins, int amount) { /** * dp[j]定义为总金额为j时最少的硬币数量 * 因为是求最小值,因此所有位置初始化为最大值 * 阅读全文
posted @ 2022-01-26 14:30 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:完全背包 class Solution { public int combinationSum4(int[] nums, int target) { /** * dp[i]定义为总和为i时排列的数量 * 初始dp[0] == 1 */ int[] dp = new int[target + 1]; 阅读全文
posted @ 2022-01-26 11:04 振袖秋枫问红叶 阅读(40) 评论(0) 推荐(0)
摘要:完全背包 class Solution { public int change(int amount, int[] coins) { /** * dp[j]定义为总金额为j时组合的方式数量 * 初始dp[0] == 1 */ int[] dp = new int[amount + 1]; dp[0] 阅读全文
posted @ 2022-01-26 10:33 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:01背包 class Solution { public int findMaxForm(String[] strs, int m, int n) { /** * 这个背包有两个维度,一个是m 一个是n,物品就是字符串,重量就是字符串中的01个数,价值就是字符串个数,都是1 * dp[i][j]定义 阅读全文
posted @ 2022-01-25 11:12 振袖秋枫问红叶 阅读(63) 评论(0) 推荐(0)
摘要:01背包 import java.util.Arrays; class Solution { public int findTargetSumWays(int[] nums, int target) { int sum = Arrays.stream(nums).sum(); /** * 可以将数组 阅读全文
posted @ 2022-01-25 09:46 振袖秋枫问红叶 阅读(50) 评论(0) 推荐(0)
摘要:01背包 import java.util.Arrays; class Solution { public int lastStoneWeightII(int[] stones) { /** * 题意可以理解为,将石头尽可能分成相等的两份,最后差值就是最小剩余重量 * 剩下的部分和《416. 分割等 阅读全文
posted @ 2022-01-24 21:39 振袖秋枫问红叶 阅读(53) 评论(0) 推荐(0)
摘要:01背包 import java.util.Arrays; class Solution { public boolean canPartition(int[] nums) { int sum = Arrays.stream(nums).sum(); /** * 如果所有数的和为奇数,则不能平分 * 阅读全文
posted @ 2022-01-24 17:06 振袖秋枫问红叶 阅读(50) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int rob(TreeNode root) { int[] dp = robOrNot(root); return Math.max(dp[0], dp[1]); } /** * 对于每个节点来说,都有两个孩子,无法用一个dp[j]同时表达 阅读全文
posted @ 2022-01-21 23:33 振袖秋枫问红叶 阅读(53) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int rob(int[] nums) { if (nums.length == 1){ return nums[0]; } int[] dp = new int[nums.length]; int max = 0; /** * 重复两次《1 阅读全文
posted @ 2022-01-21 19:49 振袖秋枫问红叶 阅读(106) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int rob(int[] nums) { if (nums.length == 1){ return nums[0]; } /** * dp[i]定义为到达第i个房屋时总共可以偷到的最大金额 * 到第一个房屋时只能偷到其本身,到第二个房屋时 阅读全文
posted @ 2022-01-21 18:37 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:动态规划 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 振袖秋枫问红叶 阅读(124) 评论(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 振袖秋枫问红叶 阅读(75) 评论(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 振袖秋枫问红叶 阅读(47) 评论(0) 推荐(0)
摘要:动态规划 import java.util.List; class Solution { public int minimumTotal(List<List<Integer>> triangle) { /** * 逆向思维 * 从下往上寻找最小路径,路径的条数越来越少,更容易找到 * 假设最后一行下 阅读全文
posted @ 2022-01-20 14:43 振袖秋枫问红叶 阅读(47) 评论(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 振袖秋枫问红叶 阅读(48) 评论(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 振袖秋枫问红叶 阅读(90) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { /** * 定义dp[i][j]为到达该坐标的路径总和 * 第一行和第一列的坐标,只能从左或者上进行访问 * 注意:如果有障碍物,那这一 阅读全文
posted @ 2022-01-19 11:11 振袖秋枫问红叶 阅读(44) 评论(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 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int minCostClimbingStairs(int[] cost) { /** * 最少有两个台阶,因此不用提前判断dp数组空指针异常的情况 * 索引从0开始 * dp[i]指的是到达第i个台阶并且向上爬所需要的最小费用 */ int 阅读全文
posted @ 2022-01-19 10:18 振袖秋枫问红叶 阅读(40) 评论(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 振袖秋枫问红叶 阅读(63) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int fib(int n) { /** * 因为n从0开始,初始了dp[0]和dp[1] * 为了避免dp[1]空指针异常,提前判断一下 */ if (n < 1){ return n; } int[] dp = new int[n + 1 阅读全文
posted @ 2022-01-18 13:59 振袖秋枫问红叶 阅读(44) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; class Solution { public int findContentChildren(int[] g, int[] s) { /** * 贪心算法 * 将胃口大小和饼干大小排序,按照最大的饼干给最大的胃口的原则,依次分配饼干 * 如果 阅读全文
posted @ 2022-01-17 21:07 振袖秋枫问红叶 阅读(45) 评论(0) 推荐(0)
摘要:回溯 class Solution { public void solveSudoku(char[][] board) { backtracking(board); } public boolean backtracking(char[][] board) { /** * 递归遍历每个空位放9个数字 阅读全文
posted @ 2022-01-17 17:43 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:回溯 import java.util.Arrays; class Solution { int sum = 0; public int totalNQueens(int n) { /** * 使用二维数组存储棋盘,最后再转换为列表 * 默认填充'.' */ char[][] chars = new 阅读全文
posted @ 2022-01-17 15:27 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:深度优先搜索 import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Solution { int m; int n; int[][] 阅读全文
posted @ 2022-01-17 14:21 振袖秋枫问红叶 阅读(60) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int m; int n; boolean[][] used; int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public void solve(char[][] board) { m = boa 阅读全文
posted @ 2022-01-16 22:44 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:深度优先搜索 import java.util.ArrayList; import java.util.HashMap; class Solution { int m; int n; boolean[][] used; int[][] move = {{0, 1}, {1, 0}, {0, -1}, 阅读全文
posted @ 2022-01-16 21:53 振袖秋枫问红叶 阅读(124) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int m; int n; boolean[][] used; int sum = 0; int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public int islandPerimeter(int 阅读全文
posted @ 2022-01-16 18:56 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int m; int n; boolean[][] used; int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public int maxAreaOfIsland(int[][] grid) { 阅读全文
posted @ 2022-01-16 17:50 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int m; int n; int sum; boolean[][] used; int[][] move = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; public int numIslands(char[][] gri 阅读全文
posted @ 2022-01-16 16:59 振袖秋枫问红叶 阅读(51) 评论(0) 推荐(0)
摘要:回溯 class Solution { int m; int n; boolean[][] used; /** * 使用辅助数组来进行上右下左遍历 */ int[][] move = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; public boolean exist(c 阅读全文
posted @ 2022-01-16 11:32 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:回溯 import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Solution { List<List<String>> list = new LinkedList<>(); public 阅读全文
posted @ 2022-01-13 18:56 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)
摘要:回溯 import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new LinkedList<>(); Linked 阅读全文
posted @ 2022-01-13 15:57 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:回溯 import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new LinkedList<>(); LinkedList<Integer> li = new Li 阅读全文
posted @ 2022-01-13 10:39 振袖秋枫问红叶 阅读(50) 评论(0) 推荐(0)
摘要:回溯 class Solution { List<List<Integer>> list = new ArrayList<>(); LinkedList<Integer> li = new LinkedList<>(); public List<List<Integer>> findSubseque 阅读全文
posted @ 2022-01-13 10:05 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要:回溯 import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list 阅读全文
posted @ 2022-01-12 21:52 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new ArrayList<>(); Link 阅读全文
posted @ 2022-01-12 21:32 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要:回溯 import java.util.Arrays; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new LinkedList<>(); Linked 阅读全文
posted @ 2022-01-12 21:02 振袖秋枫问红叶 阅读(36) 评论(0) 推荐(0)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new ArrayList<>(); Link 阅读全文
posted @ 2022-01-12 10:37 振袖秋枫问红叶 阅读(48) 评论(0) 推荐(1)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<String> list = new ArrayList<>(); LinkedList< 阅读全文
posted @ 2022-01-11 22:41 振袖秋枫问红叶 阅读(63) 评论(0) 推荐(1)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<List<String>> list = new ArrayList<>(); Linke 阅读全文
posted @ 2022-01-11 21:06 振袖秋枫问红叶 阅读(54) 评论(0) 推荐(1)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new ArrayList<>(); Link 阅读全文
posted @ 2022-01-11 16:16 振袖秋枫问红叶 阅读(47) 评论(0) 推荐(1)
摘要:回溯 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { List<List<Integer>> list = new ArrayList<>(); Link 阅读全文
posted @ 2022-01-11 15:18 振袖秋枫问红叶 阅读(58) 评论(0) 推荐(1)
摘要:深度优先搜索 import java.util.ArrayList; import java.util.List; class Solution { /** * 全局变量存储结果 */ List<String> list = new ArrayList<>(); /** * 哈希表存储数字和字母的对 阅读全文
posted @ 2022-01-10 15:19 振袖秋枫问红叶 阅读(90) 评论(0) 推荐(0)
摘要:递归 class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null){ return root; } /** * 如果大于当前节点,就在左子树寻找;小于则在右子树寻找 * 相等则分三种情况 阅读全文
posted @ 2022-01-05 17:30 振袖秋枫问红叶 阅读(46) 评论(0) 推荐(0)
摘要:中序遍历 class Solution { public boolean isValidBST(TreeNode root) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); /** * 中序遍历得到列表,判断列表 阅读全文
posted @ 2022-01-05 16:04 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:中序遍历 class Solution { public int kthSmallest(TreeNode root, int k) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); return list.get 阅读全文
posted @ 2022-01-05 15:16 振袖秋枫问红叶 阅读(19) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public TreeNode sortedArrayToBST(int[] nums) { return sortedArrayToBST(nums, 0, nums.length - 1); } /** * 每次将数组的中间元素作为根节点,这样得到的二 阅读全文
posted @ 2022-01-05 14:51 振袖秋枫问红叶 阅读(44) 评论(0) 推荐(0)