LeetCode 150.逆波兰表达式求值
题目描述
给你一个字符串数组 tokens ,表示一个根据 逆波兰表示法 表示的算术表达式。
请你计算该表达式。返回一个表示表达式值的整数。
注意:
- 有效的算符为 
'+'、'-'、'*'和'/'。 - 每个操作数(运算对象)都可以是一个整数或者另一个表达式。
 - 两个整数之间的除法总是 向零截断 。
 - 表达式中不含除零运算。
 - 输入是一个根据逆波兰表示法表示的算术表达式。
 - 答案及所有中间计算结果可以用 32 位 整数表示。
 
示例
输入:tokens = ["10","6","9","3","+","-11","","/","","17","+","5","+"]
输出:22
解释:该算式转化为常见的中缀算术表达式为:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
题目链接
https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
解题思路
用栈维护操作数部分,遇到操作符则弹出两个数字进行对应的操作,并将结果存回栈
// 7m47s
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                int a = stack.pop();
                int b = stack.pop();
                stack.push(a + b);
            } else if (token.equals("-")) {
                int a = stack.pop();
                int b = stack.pop();
                stack.push(b - a);
            } else if (token.equals("*")) {
                int a = stack.pop();
                int b = stack.pop();
                stack.push(b * a);
            } else if (token.equals("/")) {
                int a = stack.pop();
                int b = stack.pop();
                stack.push(b / a);
            } else {
                int num = Integer.parseInt(token);
                stack.push(num);
            }
        }
        return stack.pop();
    }
}
LeetCode 239.滑动窗口最大值
题目描述
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。
返回 滑动窗口中的最大值 。
示例
输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
题目链接
https://leetcode.cn/problems/sliding-window-maximum/description/
解题思路
单调队列是一种保持“单调性”的队列。
在本题中,我们需要一个单调递减队列,即:
队列头最大,越往后越小。
这样:
- 队首始终是当前窗口最大值;
 - 当新元素进来时,把比它小的都踢出去;
 - 当队首过期(滑出窗口)时,把它移除。
 
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int[] ans = new int[nums.length - k + 1];
        Deque<Integer> workQue = new LinkedList<>();
        for (int i = 0; i < nums.length; i++) {
            // 移除滑出窗口的元素
            while (!workQue.isEmpty() && workQue.peek() < i - k + 1) {
                workQue.poll();
            }
            // 维护单调递减队列(从队尾开始)
            while (!workQue.isEmpty() && nums[i] > nums[workQue.peekLast()]) {
                workQue.pollLast();
            }
            // 加入新元素
            workQue.offer(i);
            // 当窗口形成后(i >= k - 1),记录最大值
            if (i >= k - 1) {
                ans[i - k + 1] = nums[workQue.peek()];
            }
        }
        return ans;
    }
}
LeetCode 347. 前 K 个高频元素
题目描述
给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。
示例
输入:nums = [1,1,1,2,2,3], k = 2
输出:[1,2]
题目链接
https://leetcode.cn/problems/top-k-frequent-elements/description/
解题思路
使用一个小根堆维护频率前k的元素,存储结构为 Map.Entry。Map.Entry 只是一个「键值对对象」,可以把所有 entry 拿出来,放进可以排序的数据结构里。
class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> hm = new HashMap<>();
        for (int num : nums) {
            hm.put(num, hm.getOrDefault(num, 0) + 1);
        }
        // 定义小根堆
        PriorityQueue<Map.Entry<Integer, Integer>> que =
            new PriorityQueue<>((a, b) -> a.getValue() - b.getValue());
      
        for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
            que.offer(entry);
            if (que.size() > k) {
                que.poll();
            }
        }
        int[] ans = new int[k];
        for (int i = k - 1; i >= 0; i--) {
            ans[i] = que.poll().getKey();
        }
        return ans;
    }
}
                
评论