02 2023 档案
摘要:1、leetcode70 爬楼梯 需要 n 阶才能到达楼顶,每次可以爬 1 或 2 个台阶。 转化为完全背包问题 背包容量:n 物品【物品可以反复使用】 价值:1、2 重量(所占背包容量):1 代码 class Solution { public int climbStairs(int n) { /
阅读全文
摘要:1、leetcode518 零钱兑换Ⅱ【完全背包应用】【求组合数】 class Solution { public int change(int amount, int[] coins) { int[] dp = new int[amount+1]; dp[0] = 1; for(int i=0;
阅读全文
摘要:1、leetcode1049 最后一块石头的重量Ⅱ 思路 尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小 转化为01背包问题:从stones 数组中选择,凑成总和不超过sum/2的最大价值。 其中「重量」&「价值」均为数值本身。 代码 class Solution { public int l
阅读全文
摘要:1、leetcode416 分隔等和子集 转化为01背包问题 背包的体积为sum / 2 背包要放入的商品(集合里的元素)重量为 元素的数值,价值也为元素的数值 背包如果正好装满,说明找到了总和为 sum / 2 的子集。 背包中每一个元素是不可重复放入。 class Solution { publ
阅读全文
摘要:1、leetcode343 整数拆分 class Solution { public int integerBreak(int n) { // dp[i] : 对i分解得到的最大乘积 int[] dp = new int[n+1]; // 对0 1 分解无意义 => 对dp[0] dp[1] 初始化
阅读全文
摘要:1、leetcode62 不同路径 步骤 确定dp数组(dp table)以及下标的含义 dp[i] [j] : 从(0,0)到(i,j)的路径数量 确定递推公式 (i,j) 可由 (i-1,j)、(i,j-1)到达【已知:每次只能向下或者向右移动一步】 dp[i] [j] = dp[i-1] [j
阅读全文
摘要:1、leetcode738 单调递增的数字 思路 一旦出现strNum[i - 1] > strNum[i]的情况(非单调递增),首先想让strNum[i - 1]-- 从后向前遍历,就可以重复利用上次比较得出的结果了 代码 class Solution { public int monotoneI
阅读全文
摘要:1、动规理论基础 动态规划中每一个状态一定是由上一个状态推导出来的,而贪心是局部直接选最优的。 解题步骤 确定dp数组(dp table)以及下标的含义 确定递推公式 dp数组如何初始化 确定遍历顺序 举例推导dp数组 2、leetcode509 斐波那契数 思路 确定dp数组(dp table)以
阅读全文
摘要:1、leetcode435 无重叠区间 代码 class Solution { public int eraseOverlapIntervals(int[][] intervals) { Arrays.sort(intervals,(a,b)->(a[0]-b[0])); int count = 1
阅读全文
摘要:1、leetcode860 柠檬水找零 class Solution { public boolean lemonadeChange(int[] bills) { if(bills[0] != 5) { return false; } int five_nums = 0; int ten_nums
阅读全文
摘要:1、leetcode1005 K次取反后最大化的数组和 思路 局部最优:让绝对值大的负数变为正数,当前数值达到最大,整体最优:整个数组和达到最大。 代码 class Solution { //共使用两次排序 public int largestSumAfterKNegations(int[] num
阅读全文
摘要:1、leetcode122 买卖股票的最佳时机Ⅱ 思路 局部最优:收集每天的正利润,全局最优:求得最大利润。通过局部最优推出全局最优 代码 class Solution { int maxBenefit = 0; public int maxProfit(int[] prices) { for(in
阅读全文
摘要:1、leetcode455 分发饼干 class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int gIndex = 0; int sIndex = 0;
阅读全文
摘要:1、leetcode93 复原IP地址 class Solution { List<String> res = new ArrayList<>(); public boolean isValid(String s, int start, int end) { if(start > end) { re
阅读全文
摘要:1、leetcode39 组合总和 class Solution { List<Integer> path = new LinkedList<Integer>(); List<List<Integer>> res = new ArrayList<>(); int sum; public void b
阅读全文
摘要:1、leetcode216 组合总和Ⅲ class Solution { List<Integer> path = new LinkedList<Integer>();// 符合条件的结果 List<List<Integer>> res = new ArrayList<>();// 存放结果集 in
阅读全文
摘要:1、leetcode77 组合 class Solution { List<Integer> path = new LinkedList<Integer>();// 用来存放符合条件结果 List<List<Integer>> res = new ArrayList<>();// 存放符合条件结果的
阅读全文
摘要:1、669 修剪二叉搜索树 class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if(root == null){ return null; } if(root.val < low){ return
阅读全文
摘要:1、235. 二叉搜索树的最近公共祖先 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val > p.val && root.val > q
阅读全文
摘要:1、530.二叉搜索树的最小绝对差 class Solution { private int res = Integer.MAX_VALUE; private TreeNode pre; public void traversal(TreeNode node) { if(node == null)
阅读全文
摘要:1、654 最大二叉树 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { return traversal(nums, 0, nums.length); } public TreeNode traver
阅读全文
摘要:1、leetcode513 找树左下角的值 递归法 目标:在树的最后一行找到最左边的值 保证优先左边搜索,【前中后序都可以,因为没有中间节点的处理逻辑】 然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。 递归三部曲: 确定递归函数的参数和返回值 参数必须有要遍历的树的根节点,还有就是一个
阅读全文