Stack
1 2 3 4 5 6 7 8 9 10
| Stack<E> stack = new Stack<>();
stack.push(E item);
stack.pop();
stack.peek();
stack.empty();
|
Queue
1 2 3 4 5 6 7 8 9 10
| Queue<E> queue = new LinkedList<>();
queue.offer();
queue.poll();
queue.peek();
queue.isEmpty();
|
20. 有效的括号
20. 有效的括号 - 力扣(LeetCode)
解题思路:从左向右遍历,遇到左括号入栈,遇到与栈顶元素匹配的右括号将栈顶弹出。否则不匹配或最后栈内不为空说明无效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(' || c == '{' || c == '[') { stack.push(c); continue; } else if (stack.isEmpty()) return false; if (c == ')' && stack.peek() == '(') stack.pop(); else if (c == '}' && stack.peek() == '{') stack.pop(); else if (c == ']' && stack.peek() == '[') stack.pop(); else return false; } if (!stack.isEmpty()) return false; return true; } }
|
239. 滑动窗口最大值
239. 滑动窗口最大值 - 力扣(LeetCode)
暴力方法:每次从窗口中找出最大值,时间复杂度是O(n x k)
解决思路:利用队列,队列里面维护可能成为窗口最大值的元素,同时保证队列中的元素是从大到小的,即单调队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
class MyQueue { Deque<Integer> deque = new LinkedList<>(); void poll(int val) { if (!deque.isEmpty() && val == deque.peek()) { deque.poll(); } } void add(int val) { while (!deque.isEmpty() && val > deque.getLast()) { deque.removeLast(); } deque.add(val); } int peek() { return deque.peek(); } }
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length == 1) { return nums; } int len = nums.length - k + 1; int[] res = new int[len]; int num = 0; MyQueue myQueue = new MyQueue(); for (int i = 0; i < k; i++) { myQueue.add(nums[i]); } res[num++] = myQueue.peek(); for (int i = k; i < nums.length; i++) { myQueue.poll(nums[i - k]); myQueue.add(nums[i]); res[num++] = myQueue.peek(); } return res; } }
|
347.前 K 个高频元素
347. 前 K 个高频元素 - 力扣(LeetCode)
解题思路:
- 要统计元素出现频率
- 对频率排序
- 找出前K个高频元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); int[] ans = new int[k]; for (int num : nums) { map.put(num, map.getOrDefault(num, 0) + 1); } List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(map.entrySet());
entryList.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue())); for (int i = 0; i < k; i++) { ans[i] = entryList.get(i).getKey(); } return ans; } }
|