力扣爆刷第140天之栈与队列七连刷(表达式求值、高频元素)
文章目录
一、232. 用栈实现队列
题目链接:https://leetcode.cn/problems/implement-queue-using-stacks/description/
思路:用两个栈模拟一个队列,只需要一个栈只用于接收元素,另一个栈只用于弹出元素,只要添加元素,就添加到第一个栈,只要弹出元素,就看看第二个栈是否为空,为空就把第一个栈内的元素都放入第二个栈内,就可以,这样就把元素从先进后出,变成了先进先出。
class MyQueue {
Deque<Integer> stack1 = new LinkedList<>();
Deque<Integer> stack2 = new LinkedList<>();
public MyQueue() {
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
二、225. 用队列实现栈
题目链接:https://leetcode.cn/problems/implement-stack-using-queues/description/
思路:用队列实现栈,只需要一个队列即可,添加元素正常添加,弹出元素,要先把n-1个元素弹出再添加回来,此时队头元素就是之前队尾元素,就实现了后进先出。
class MyStack {
LinkedList<Integer> queue = new LinkedList<>();
public MyStack() {
}
public void push(int x) {
queue.add(x);
}
public int pop() {
int size = queue.size();
for(int i = 1; i < size; i++) {
queue.add(queue.poll());
}
return queue.poll();
}
public int top() {
int size = queue.size();
for(int i = 1; i < size; i++) {
queue.add(queue.poll());
}
int t = queue.peek();
queue.add(queue.poll());
return t;
}
public boolean empty() {
return queue.isEmpty();
}
}
三、20. 有效的括号
题目链接:https://leetcode.cn/problems/valid-parentheses/description/
思路:很典型的一道题,只需要遇到左括号,就添加右括号,遇到有括号,就比较栈顶元素是否相等,不等则无效。
class Solution {
public boolean isValid(String s) {
LinkedList<Character> stack = new LinkedList<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(') {
stack.push(')');
}else if(c == '[') {
stack.push(']');
}else if(c == '{') {
stack.push('}');
}else{
if(stack.isEmpty() || stack.peek() != c) return false;
stack.poll();
}
}
return stack.isEmpty();
}
}
四、1047. 删除字符串中的所有相邻重复项
题目链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/
思路:删除相邻的重复项,只需要使用栈,如果栈空或者栈顶元素不等于当前元素,就可以直接添加,如果栈非空,并且栈顶元素等于当前元素,则直接弹出栈顶元素。
class Solution {
public String removeDuplicates(String s) {
LinkedList<Character> stack = new LinkedList<>();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(!stack.isEmpty() && stack.peek() == c) {
stack.poll();
}else{
stack.push(c);
}
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()) {
sb.append(stack.poll());
}
return sb.reverse().toString();
}
}
五、150. 逆波兰表达式求值
题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
思路:这也是一个典型的题目了,只需要使用栈,只要是数字就加入栈内,只要是±*/的符合就把栈顶的两个元素出栈,运算后再添加回去。
class Solution {
public int evalRPN(String[] tokens) {
LinkedList<Integer> stack = new LinkedList<>();
for(String s : tokens) {
if("+".equals(s)) {
int right = stack.poll();
stack.push(stack.poll() + right);
}else if("-".equals(s)) {
int right = stack.poll();
stack.push(stack.poll() - right);
}else if("*".equals(s)) {
int right = stack.poll();
stack.push(stack.poll() * right);
}else if("/".equals(s)) {
int right = stack.poll();
stack.push(stack.poll() / right);
}else{
stack.push(Integer.valueOf(s));
}
}
return stack.pop();
}
}
六、239. 滑动窗口最大值
题目链接:https://leetcode.cn/problems/sliding-window-maximum/description/
思路:求滑动窗口的最大值,直接自己写一个队列类,在类的内部提供维护滑动窗口的函数,只要是添加元素,小于当前元素的都要出队,此时队列内部保存的正是单调递减的元素,要获取最大值即是队列头部,要删除元素也仅仅是与队头做比较,相当则删除。
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
int[] result = new int[nums.length - k + 1];
MyQueue queue = new MyQueue();
int j = 0;
for(int i = 0; i < nums.length; i++) {
queue.add(nums[i]);
if(i+1 >= k) {
result[j++] = queue.getMax();
queue.remove(nums[i-k+1]);
}
}
return result;
}
}
class MyQueue{
LinkedList<Integer> queue;
public MyQueue() {
queue = new LinkedList<>();
}
void add(int v) {
while(!queue.isEmpty() && queue.peekLast() < v) {
queue.pollLast();
}
queue.addLast(v);
}
void remove(int v) {
if(!queue.isEmpty() && queue.peekFirst() == v) {
queue.pollFirst();
}
}
int getMax() {
return queue.peekFirst();
}
}
七、347. 前 K 个高频元素
题目链接:https://leetcode.cn/problems/top-k-frequent-elements/description/
思路:本题很简单,使用map记录元素和出现次数,然后使用优先级队列,记录再弹出前k个元素即可。
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> b[1]-a[1]);
Set<Integer> set = map.keySet();
for(int i : set) {
int[] temp = new int[2];
temp[0] = i;
temp[1] = map.get(i);
queue.add(temp);
}
int[] result = new int[k];
for(int i = 0; i < k; i++) {
result[i] = queue.poll()[0];
}
return result;
}
}