上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 21 下一页
摘要: 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 风乐 阅读(13) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 要点是明白中序和后序如何构造二叉树的,并且需要理清当前递归函数的语义,不要一开始就陷入细节,而是思考整棵树与其左右子树的关 阅读全文
posted @ 2024-05-05 11:35 风乐 阅读(43) 评论(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)
摘要: https://leetcode.cn/problems/sliding-window-maximum/ 简单的滑动窗口,但是与ACM模式的维护数组不同,在leetcode定义单调队列类更加方便 class MyQueue{ // 单调队列实现,递减 Deque<Integer> deque = n 阅读全文
posted @ 2024-04-30 22:27 风乐 阅读(17) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/minimum-path-cost-in-a-grid 思路很简单,实现很恶心,主要由于我习惯于dp数组从1开始而不是0,这样在推导状态时由于grid是从0,0开始,而循环是从dp数组的1,1开始,因此在方程中一旦涉及到grid就需要把i,j 阅读全文
posted @ 2024-04-28 16:16 风乐 阅读(19) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/merge-intervals/?envType=study-plan-v2&envId=top-100-liked 合并区间练习题 typedef pair<int,int> PII; vector<PII> segs; class Sol 阅读全文
posted @ 2024-04-25 11:30 风乐 阅读(13) 评论(0) 推荐(0)
摘要: https://leetcode.cn/problems/edit-distance/description/?envType=study-plan-v2&envId=top-100-liked 这是一个难题,关于序列DP的,官方的题解较为难懂,这里有一位前辈解释的很好 这里的状态定义是: dp[i 阅读全文
posted @ 2024-04-23 17:25 风乐 阅读(10) 评论(0) 推荐(0)
摘要: 题面如下: https://www.acwing.com/problem/content/1252/ 这题需要从点出发,不需要管是红线还是蓝线,划线了就得合并两点到同一集合,只要当前线还未画之前,这两个端点是同一集合,那么画完后就是一个连通块,线组成的集合构成连通意味着成圈了,即得到答案 #incl 阅读全文
posted @ 2024-04-19 11:12 风乐 阅读(27) 评论(0) 推荐(0)
摘要: 题面如下: https://www.acwing.com/problem/content/530/ 大致思路是:合并所有连接的空洞,判断下表面的空洞和上表面的空洞是否是同一集合集合 #include<iostream> #include<cstring> #include<cstdio> #incl 阅读全文
posted @ 2024-04-19 11:05 风乐 阅读(11) 评论(0) 推荐(0)
上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 21 下一页