摘要: 2024年7月30日 题122. 买卖股票的最佳时机 II 上涨就买,下跌就不买。 class Solution { public int maxProfit(int[] prices) { int sum = 0; for(int i=1;i<prices.length;i++){ sum+=pr 阅读全文
posted @ 2024-07-30 11:51 hailicy 阅读(5) 评论(0) 推荐(0) 编辑
摘要: 2024年7月29日 题455. 分发饼干 先排序,然后依次分发即可。 class Solution { public int findContentChildren(int[] g, int[] s) { //对于每个孩子胃口,从小到大分配,且给尽可能少的饼干 Arrays.sort(g); Ar 阅读全文
posted @ 2024-07-30 11:09 hailicy 阅读(3) 评论(0) 推荐(0) 编辑
摘要: 2024年7月27日 题46. 全排列 继续回溯。 class Solution { List<List<Integer>> res; List<Integer> path; int[] used; int[] nums; public List<List<Integer>> permute(int 阅读全文
posted @ 2024-07-28 15:23 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月26日 题78. 子集 对于每个元素,都有选或者不选两种选择 class Solution { List<List<Integer>> res; List<Integer> path; int[] nums; public List<List<Integer>> subsets(int 阅读全文
posted @ 2024-07-28 14:32 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月25日 题39. 组合总和 由于每个元素可以用多次,要想到在每次递归里还要循环即可。 代码首先给各个候选排序,从小到大依次提高门槛,每次回溯就提高index。 class Solution { List<List<Integer>> res; List<Integer> path; i 阅读全文
posted @ 2024-07-27 14:10 hailicy 阅读(4) 评论(0) 推荐(0) 编辑
摘要: 2024年7月24日 回溯入门 用for来遍历每个元素,然后进入递归。 题77. 组合 给出n和k,可选元素1到n,k是组合个数。 易错:添加进外层list时要new一个,否则后续修改会影响已经添加的内层list class Solution { List<List<Integer>> list1; 阅读全文
posted @ 2024-07-24 10:19 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月23日 题669. 修剪二叉搜索树 暴力做法是找出所有不符合的节点再一一删除。 import java.util.*; class Solution { ArrayList<Integer> list; public TreeNode trimBST(TreeNode root, in 阅读全文
posted @ 2024-07-24 08:56 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月22日 题235. 二叉搜索树的最近公共祖先 普通解法还是和普通的祖先一样求即可,再依据搜索树特性进行剪枝即可加速。 import java.util.*; class Solution { Vector<TreeNode> vec1; Vector<TreeNode> vec2; i 阅读全文
posted @ 2024-07-23 16:05 hailicy 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 2024年7月20日 题530. 二叉搜索树的最小绝对差 使用递归获取中序遍历,然后遍历一遍vector即可得到结果。 import java.util.*; class Solution { Vector<Integer> vec; public int getMinimumDifference( 阅读全文
posted @ 2024-07-23 15:23 hailicy 阅读(3) 评论(0) 推荐(0) 编辑
摘要: 2024年7月19日 题654. 最大二叉树 熟练运用递归即可 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { int maxNum = Integer.MIN_VALUE; int flag=-1; 阅读全文
posted @ 2024-07-22 16:16 hailicy 阅读(5) 评论(0) 推荐(0) 编辑