随笔分类 -  算法

上一页 1 2 3 4 5 6 7 8 ··· 10 下一页
随手写的
摘要:https://leetcode.cn/problems/merge-intervals/description/ 经典题合并区间 class Solution { public int[][] merge(int[][] intervals) { Arrays.sort(intervals,(a, 阅读全文
posted @ 2024-08-29 18:36 风乐 阅读(20) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/partition-labels/description/ 听说这题是字节广告一面的题有两种做法,但是思路大致相同,主要思路是先求出所有字符的出现的最远距离,然后不断往后遍历,更新当前片段的最远距离 若是第一种做法,就是放在另一个循环中,不断 阅读全文
posted @ 2024-08-29 16:43 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/non-overlapping-intervals/description/ 贪心:思路是更新重叠的区间 class Solution { public int eraseOverlapIntervals(int[][] intervals) 阅读全文
posted @ 2024-08-29 15:38 风乐 阅读(17) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/ 思路是排序,方便计算气球重叠,难点是在重叠时更新右边界,更新为 两个区间的最右重合点,因为这个点是最少一支箭就可以射掉两个气球的最 阅读全文
posted @ 2024-08-29 14:55 风乐 阅读(29) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/queue-reconstruction-by-height/submissions/ 贪心:大致思路是排序,但是可以先排k再排h,或者是先排h再排k,这里只能穷举,发现第一种不合法于是使用第二种,先按照h排序,然后由于h有序了(从大到小降序 阅读全文
posted @ 2024-08-28 02:43 风乐 阅读(12) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/lemonade-change/description/ 贪心: 由于 10 美元钞票只能用于 20 美元的找零,而 5 美元钞票既可以用于 20 美元的找零,又可以用于 10 美元的找零,更加通用(使用场景更多),所以如果可以用 10 美元 阅读全文
posted @ 2024-08-28 02:05 风乐 阅读(7) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/candy/description/ 贪心,策略是确定一侧的正确性,再确定另一侧的正确性,最后综合作为正确答案,其中先确定一侧的正确性是局部最优,确定两侧的正确性的局部最优,且找不到反例就可以推出全局最优答案 class Solution { 阅读全文
posted @ 2024-08-27 21:47 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/gas-station/ 贪心,原理是: 如果x能到达y,不能到达y+1,那么x-y之间的点也不可能到达y+1:z为xy之间一点,从x开始到z(在z加油前),剩余油量一定大等于0,但是从z开始的话,起始油量一定等于0 >> 起始油量大等于0都 阅读全文
posted @ 2024-08-24 00:03 风乐 阅读(16) 评论(0) 推荐(0)
摘要:https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3?tpId=37&tqId=21331&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta% 阅读全文
posted @ 2024-06-15 01:44 风乐 阅读(30) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/combination-sum-ii/description/ class Solution { List<List<Integer>> res = new ArrayList<>(); LinkedList<Integer> path = 阅读全文
posted @ 2024-05-16 12:41 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/combination-sum/description/ 两种搜索思路 一种是选或不选,搜索树是一颗二叉树另一种是选哪个数,是一个n叉树 二叉 class Solution { List<List<Integer>> res = new Ar 阅读全文
posted @ 2024-05-15 22:40 风乐 阅读(7) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/ 要点是分割左右区间,并且分割时需要注意left和right相加可能会超过int,但是本题不需要 class Solution { 阅读全文
posted @ 2024-05-15 16:51 风乐 阅读(11) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/trim-a-binary-search-tree/description/ 要点是区分在区间左边还是右边,在区间左边那么右子树也还有必要去查找删除,右边同理,返回的是删除后新树的根节点 要注意函数要实现单层逻辑和完成闭环语义 class S 阅读全文
posted @ 2024-05-11 14:03 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/delete-node-in-a-bst/ 要点是确定函数语义,单层递归逻辑,确定好有返回值的这种写法,分析出5种情况,递归的时候接收好递归的返回的新树的根节点即可 class Solution { // 函数语义(单层递归逻辑):查找要删除 阅读全文
posted @ 2024-05-11 12:31 风乐 阅读(29) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/insert-into-a-binary-search-tree/ 推荐使用没有返回值的方法,因为比较好想233 递归法: class Solution { public: TreeNode* pre=nullptr; TreeNode* i 阅读全文
posted @ 2024-05-10 11:14 风乐 阅读(9) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/ 明确函数的返回值和参数,返回最近公共祖先,没有就返回null 单层递归逻辑是:若按搜索树条件,递归左子树未查询到,递归右子树也未查询到,那么意味着 阅读全文
posted @ 2024-05-08 23:43 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/ 要点是后序遍历,这样就可以从树底开始搜索,寻找最近公共祖先 class Solution { public: // 返回的是最近公共祖先 阅读全文
posted @ 2024-05-08 22:45 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 思路和106. 从中序与后序遍历序列构造二叉树相同 /** * Definition for a binary tree 阅读全文
posted @ 2024-05-05 16:35 风乐 阅读(12) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 要点是明白中序和后序如何构造二叉树的,并且需要理清当前递归函数的语义,不要一开始就陷入细节,而是思考整棵树与其左右子树的关 阅读全文
posted @ 2024-05-05 11:35 风乐 阅读(42) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/top-k-frequent-elements/description/ 可以考虑使用HashMap+排序,不过直接使用优先队列也挺不错,可以使用大顶堆或小顶堆 class Solution { public int[] topKFreque 阅读全文
posted @ 2024-04-30 23:15 风乐 阅读(11) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 8 ··· 10 下一页