一、150. 逆波兰表达式求值
题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
文章讲解:https://programmercarl.com/0150.%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%B1%82%E5%80%BC.html
视频讲解:https://www.bilibili.com/video/BV1kd4y1o7on
1.1 初见思路
- 就是用栈,碰到符号,就弹出最上面的2个元素,计算完后吧结果再push进栈
1.2 具体实现
class Solution {public int evalRPN(String[] tokens) {Deque<Integer> deque = new LinkedList();for (int i = 0; i < tokens.length; i++) {String token = tokens[i];if(token.equals("+")){int num2 = deque.pop();int num1 = deque.pop();deque.push(num1+num2);}else if(token.equals("-")){int num2 = deque.pop();int num1 = deque.pop();deque.push(num1-num2);}else if(token.equals("*")){int num2 = deque.pop();int num1 = deque.pop();deque.push(num1*num2);}else if(token.equals("/")){int num2 = deque.pop();int num1 = deque.pop();deque.push((num1/num2));}else{deque.push(Integer.parseInt(token));}}return deque.pop();}
}
1.3 重难点
二、 239. 滑动窗口最大值
题目链接:https://leetcode.cn/problems/sliding-window-maximum/
文章讲解:https://programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html
视频讲解:https://www.bilibili.com/video/BV1XS4y1p7qj
2.1 初见思路
- 使用双端队列,得知道移动时,应该去除哪个值,增加哪个值
- 记录当前窗口内最大值,若移除了最大值,则重新比一遍窗口内的最大值
- 若未移除最大值,比较新增值和最大值,更新最大值
2.2 具体实现
class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int[] res = new int[nums.length-k+1];int maxNum=Integer.MIN_VALUE;LinkedList<Integer> linkedList = new LinkedList<>();for(int i=0;i<k;i++){linkedList.addLast(nums[i]);maxNum=Math.max(nums[i],maxNum);}res[0]=maxNum;for(int i=k;i<nums.length;i++){int outNum = linkedList.peekFirst();//若未移除最大值,比较新增值和最大值if(outNum!=maxNum){maxNum=maxNum<nums[i]?nums[i]:maxNum;linkedList.pollFirst();linkedList.addLast(nums[i]);}else{linkedList.pollFirst();linkedList.addLast(nums[i]);//重新比一遍窗口内的最大值maxNum = getMax(linkedList);}res[i-k+1]=maxNum;}return res;}public int getMax(LinkedList<Integer> list){int max=Integer.MIN_VALUE;for (Integer integer : list) {max=Math.max(max,integer);}return max;}
}
上面的实现方式会在k很大的时候超时,因为getMax()方法会遍历,所以会超时;
得使用双端队列构建出有序队列
class Solution {public int[] maxSlidingWindow(int[] nums, int k) {ArrayDeque<Integer> deque = new ArrayDeque<>();int n = nums.length;int[] res = new int[n - k + 1];int idx = 0;for(int i = 0; i < n; i++) {// 根据题意,i为nums下标,是要在[i - k + 1, i] 中选到最大值,只需要保证两点// 1.队列头结点需要在[i - k + 1, i]范围内,不符合则要弹出while(!deque.isEmpty() && deque.peek() < i - k + 1){deque.poll();}// 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {deque.pollLast();}deque.offer(i);// 因为单调,当i增长到符合第一个k范围的时候,每滑动一步都将队列头节点放入结果就行了if(i >= k - 1){res[idx++] = nums[deque.peek()];}}return res;}
}
2.3 重难点
- 要会使用双端队列构建出有序队列
三、 347. 前 K 个高频元素
题目链接:https://leetcode.cn/problems/top-k-frequent-elements/description/
文章讲解:https://programmercarl.com/0347.%E5%89%8DK%E4%B8%AA%E9%AB%98%E9%A2%91%E5%85%83%E7%B4%A0.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
视频讲解:https://www.bilibili.com/video/BV1Xg41167Lz
3.1 初见思路
3.2 具体实现
class Solution {public int[] topKFrequent(int[] nums, int k) {HashMap<Integer,Integer> map = new HashMap<>();for(int num:nums){map.put(num,map.getOrDefault(num,0)+1);}PriorityQueue<Integer> queue = new PriorityQueue<>((p1,p2)->{return map.get(p1)-map.get(p2);});for(Map.Entry<Integer,Integer> entry:map.entrySet()){//queue不满的时候直接插入if(queue.size()<k){queue.add(entry.getKey());}//queue满了就需要移除后再插入else if(entry.getValue()>map.get(queue.peek())){queue.remove();queue.add(entry.getKey());}}int[] res = new int[queue.size()];int index=0;for(int i=k-1;i>=0;i--){//依次弹出小顶堆,先弹出的是堆的根,出现次数少,后面弹出的出现次数多res[i] = queue.poll();}return res;}
}