随笔分类 - 算法训练营
摘要:1、动规理论基础 动态规划中每一个状态一定是由上一个状态推导出来的,而贪心是局部直接选最优的。 解题步骤 确定dp数组(dp table)以及下标的含义 确定递推公式 dp数组如何初始化 确定遍历顺序 举例推导dp数组 2、leetcode509 斐波那契数 思路 确定dp数组(dp table)以
阅读全文
摘要:1、leetcode738 单调递增的数字 思路 一旦出现strNum[i - 1] > strNum[i]的情况(非单调递增),首先想让strNum[i - 1]-- 从后向前遍历,就可以重复利用上次比较得出的结果了 代码 class Solution { public int monotoneI
阅读全文
摘要: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 找树左下角的值 递归法 目标:在树的最后一行找到最左边的值 保证优先左边搜索,【前中后序都可以,因为没有中间节点的处理逻辑】 然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。 递归三部曲: 确定递归函数的参数和返回值 参数必须有要遍历的树的根节点,还有就是一个
阅读全文
摘要:1、leetcode110 平衡二叉树 平衡二叉树:一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。 递归法 明确递归函数的参数和返回值 参数:当前传入节点。 返回值:以当前传入节点为根节点的树的高度,返回-1 来标记已经不符合平衡树的规则。 终止条件 遇到空节点了为终止,返回0,表示当前
阅读全文
摘要:1、104.二叉树的最大深度 递归法 二叉树的最大深度 ==》根节点的高度 通过后序(左右中)求得根节点高度来求得二叉树最大深度。 递归三要素 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。 确定终止条件:如果为空节点的话,就返回0,表示高度为0
阅读全文
摘要:1、层序遍历 102.二叉树的层序遍历 class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> resList = new ArrayList<List<Integer>>
阅读全文
摘要:1、二叉树理论基础篇 二叉树的种类 满二叉树:二叉树的所有叶子节点都在最后一层,并且节点总数为2^n-1,n为层数【从1 开始】 完全二叉树:二叉树的所有叶子节点都在最后一层或者倒数第二层,且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,则该二叉树为完全二叉树。 二叉搜索树(二叉排序
阅读全文

浙公网安备 33010602011771号