上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 22 下一页
摘要: 题目链接 102. 二叉树的层序遍历 思路 树的层次遍历模板题,需要使用一个变量来记录每一层的结点个数 代码 class Solution { public List<List<Integer>> levelOrder(TreeNode root) { if(root == null){ retur 阅读全文
posted @ 2023-01-11 15:15 Frodo1124 阅读(37) 评论(0) 推荐(0) 编辑
摘要: 题目链接 34. 在排序数组中查找元素的第一个和最后一个位置 思路 转自:林小鹿的题解 两套二分查找模板,分别用来查找左边界和右边界 int bsearch_1(int l, int r) { while (l < r) { int mid = (l + r)/2; if (check(mid)) 阅读全文
posted @ 2023-01-11 11:23 Frodo1124 阅读(50) 评论(0) 推荐(0) 编辑
摘要: 题目链接 295. 数据流的中位数 思路 维护两个优先队列,分别装载排序后数据流左边的数和数据流右边的数,其中 left 为大顶堆,right 为小顶堆。如果元素个数是奇数,则把中位数放到 left 中。 代码 class MedianFinder { PriorityQueue<Integer> 阅读全文
posted @ 2023-01-10 14:12 Frodo1124 阅读(23) 评论(0) 推荐(0) 编辑
摘要: 题目链接 692. 前K个高频单词 思路 还是与前k大问题一样,只不过需要注意一下字典序的问题。 代码 class Solution{ public List<String> topKFrequent(String[] words, int k){ HashMap<String, Integer> 阅读全文
posted @ 2023-01-09 09:36 Frodo1124 阅读(17) 评论(0) 推荐(0) 编辑
摘要: 题目链接 23. 合并K个升序链表 思路 把全部结点放入优先队列中,然后再依次组成新链表 代码 class Solution { public ListNode mergeKLists(ListNode[] lists) { PriorityQueue<Integer> listNodePriori 阅读全文
posted @ 2023-01-08 14:37 Frodo1124 阅读(21) 评论(0) 推荐(0) 编辑
摘要: 题目链接 347. 前 K 个高频元素 思路 前k大模板题 代码 class Solution{ public int[] topKFrequent(int[] nums, int k){ PriorityQueue<Map.Entry<Integer, Integer>> priorityQueu 阅读全文
posted @ 2023-01-08 11:46 Frodo1124 阅读(11) 评论(0) 推荐(0) 编辑
摘要: 题目链接 973. 最接近原点的 K 个点 思路 使用优先队列处理前k大问题 代码 class Solution { class Node{ int x; int y; double distance; } public int[][] kClosest(int[][] points, int k) 阅读全文
posted @ 2023-01-08 11:11 Frodo1124 阅读(31) 评论(0) 推荐(0) 编辑
摘要: 题目链接 299. 猜数字游戏 思路 建立两个哈希表分别存储 secret 和 guess 中不是bulls的数字出现次数。 代码 class Solution{ public String getHint(String secret, String guess){ int[] secretAlph 阅读全文
posted @ 2023-01-07 14:45 Frodo1124 阅读(27) 评论(0) 推荐(0) 编辑
摘要: 题目链接 350. 两个数组的交集 II 思路 建立两个哈希表分别统计 nums1 和 nums2 中每个数字出现的个数,然后同时遍历两个哈希表,对两个对位元素取其最小值 count,将 count 数量的数字放入结果数组中。 代码 class Solution { public int[] int 阅读全文
posted @ 2023-01-07 13:04 Frodo1124 阅读(23) 评论(0) 推荐(0) 编辑
摘要: 题目链接 49. 字母异位词分组 思路 如果一对字符串是字母异位词,那么他们经过排序之后,应该是相等的。 利用这一特点,我们通过哈希表建立排序后字符串到原字符串列表的映射,不断把 strs 中的字符串加入到合适的链表中。 最后遍历哈希表,可以得到最终结果。 代码 class Solution{ pu 阅读全文
posted @ 2023-01-07 12:44 Frodo1124 阅读(29) 评论(0) 推荐(0) 编辑
上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 22 下一页