随笔分类 - 算法
刷题力扣
    
摘要:思路:可以把交易日分为上涨交易日与下降交易日,假如今天比昨天贵,就叫上涨。用贪心策略,每个上涨交易日都买,下降交易日都不买 class Solution {//贪心策略,所有上涨交易日都买,所有下降交易日都不卖 public int maxProfit(int[] prices) { int len
        阅读全文
                
摘要:思路:可以找出数组里每一个元素右侧的最大值,再遍历整个数组,找当前元素右侧最大值和当前元素数值的差,最大的就是要求的结果。 class Solution { public int maxProfit(int[] prices) { int[] rightMax=new int[prices.leng
        阅读全文
                
摘要:https://leetcode-cn.com/problems/triangle/solution/zi-di-xiang-shang-dong-tai-gui-hua-lei-si-yu-cong-/ 思路: 以[[2],[3,4],[6,5,7],[4,1,8,3]]为例: 假设从倒数第二列开
        阅读全文
                
摘要:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/236ti-er-cha-shu-de-zui-jin-gong-gong-zu-xian-by-i/ 最近公共祖先的定义: 设节点 r
        阅读全文
                
摘要:https://leetcode-cn.com/problems/pascals-triangle-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--28/ 思路: 注意一下给出的用例,最上方节点算是第0行,所以说,第三行的这个3
        阅读全文
                
摘要:思路: List有个get(i),函数,求的是list里面,下标为i的这个元素,注意这一点!很有用 此外,每一行的第一个与最后一个元素一定是1,不满足那个规则,所以说要单独处理 class Solution { public List<List<Integer>> generate(int numR
        阅读全文
                
摘要:class Solution {//看笔记,散列表 public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> set=new HashSet<Integer>();//散列表,不能存储相同元素,而且存储元素顺序不
        阅读全文
                
摘要:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node-ii/solution/di-gui-fa-jian-dan-yi-dong-ban-xin-shou-kan-by-lov/ 思路:要用到一个函
        阅读全文
                
摘要:思路: (1)因为是一个完美二叉树,也就是,一个节点要么没有左右子节点,要么左右子节点都有 (2)假如根节点为空。或者根节点左子树为空,不需要进行算法,返回root (3)此时根节点一定有子树,把根的左子节点和右子节点连接起来 (4)之后判断根节点是否有next(和根同一行的节点),有的话,还是因为
        阅读全文
                
摘要:首先,可以排序,之后遍历,不过时间复杂度是nlogn,代码如下,不细说 class Solution { public int singleNumber(int[] nums) { Arrays.sort(nums); if(nums.length==1)return nums[0]; int le
        阅读全文
                
摘要:https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/huan-xing-lian-biao-ii-by-leetcode/ 思路: (1)首先判断有没有环,把quick与slow设置为head,当满足2的条件时,slow每次移
        阅读全文
                
摘要:快慢指针即可 public class Solution { public boolean hasCycle(ListNode head) { if(head==null||head.next==null) return false; ListNode slow=head; ListNode qui
        阅读全文
                
摘要:思路: class Solution { int maxDepth = -1; int sum = 0; public int deepestLeavesSum(TreeNode root) { return dfs(root, 0); } private int dfs(TreeNode root
        阅读全文
                
摘要:思路:明显可以使用回溯法,和112题很像,先把当前根节点选中,加入temp,之后使用sum1减去根节点数值,然后到了叶子结点,判断sum1是否为零,来判断当前路径是不是符合要求。 遇到符合要求的叶子结点,把当前的路径加入res里面。 假如当前根节点不是叶子结点,递归调用函数处理左右子树 对左右子树的
        阅读全文
                
摘要:思路: 最直接的方法就是利用递归,遍历整棵树:如果当前节点不是叶子,对它的所有孩子节点,递归调用 hasPathSum 函数,其中 sum 值减去当前节点的权值;如果当前节点是叶子,检查 sum 值是否为 0,也就是是否找到了给定的目标和。 class Solution {//看笔记,思路 publ
        阅读全文
                
摘要:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/solution/li-jie-zhe-dao-ti-de-jie-shu-tiao-jian-by-user7208/ 思路:有个特殊情况,比如树是1,2.这样的话,根节点为
        阅读全文
                
摘要:https://blog.csdn.net/u010983881/article/details/78896293
        阅读全文
                
摘要:https://www.cnblogs.com/chengxiao/p/6129630.html
        阅读全文
                
摘要:很简单,先从上到下层次遍历, Collections.reverse(res);反转List就行 class Solution {//先层次遍历,每一层的结果放在一个集合里,最后把大的集合翻转过来 public List<List<Integer>> levelOrderBottom(TreeNod
        阅读全文
                
摘要:二叉树后序遍历,还有107二叉树层次遍历都用到了这个函数! https://www.cnblogs.com/ywb2018/p/9922829.html
        阅读全文
                
                    
                
浙公网安备 33010602011771号