leetcode-zgd-day11-232.用栈实现队列/225.用队列实现栈/20.有效的括号/1047.删除字符串中的所有相邻重复项

574 阅读1分钟

232.用栈实现队列

思路:

需要特殊留意的点就是在于需要弹出元素的时候,如果stackout中没有元素,就需要从stackin中拿到元素。将stackin中的元素一次性的搬运到stackout中。

 class MyQueue {
 ​
          Stack<Integer> stackin;
          Stack<Integer> stackout;
 ​
     public MyQueue() {
         stackin = new Stack<>();
         stackout = new Stack<>();
     }
     
     public void push(int x) {
         stackin.push(x);
     }
     
     public int pop() {
         if(stackout.isEmpty()){
             // TODO: 2022/10/1 stack1全部移动到stack2
             move();
         }
         return stackout.pop();
     }
     
     public int peek() {
         if(stackout.isEmpty()){
             // TODO: 2022/10/1 stack1全部移动到stack2
             move();
         }
         return stackout.peek();
     }
     
     public boolean empty() {
         return stackout.isEmpty() && stackin.isEmpty();
     }
 ​
     public void move(){
         while(!stackin.isEmpty()){
             int temp = stackin.peek();
             stackin.pop();
             stackout.push(temp);
         }
     }
 }

225.用队列实现栈

关键点:

队列2就是用来倒腾元素的,当需要pop或者需要top的时候,就把队列1除最后一个元素都移到队列2,这样就可以操作想要的元素。然后再将所有移到队列2的元素再移回来。num用来记录队列1中的元素个数。

 class MyStack {
 ​
          Queue<Integer> queue1;
          Queue<Integer> queue2;
          int num;
 ​
     public MyStack() {
         queue1 = new LinkedList<>();
         queue2 = new LinkedList<>();
         num = 0;
     }
     
     public void push(int x) {
         // 直接放入队列1
         queue1.offer(x);
         num++;
     }
     
     public int pop() {
         while(num-- > 1){
             int temp = queue1.peek();
             queue1.poll();
             queue2.offer(temp);
         }
         int ans = queue1.peek();
         queue1.poll();
         while(!queue2.isEmpty()){ // 把元素都运回来
             int temp = queue2.peek();
             queue2.poll();
             queue1.offer(temp);
             num++;
         }
         return ans;
     }
     
     public int top() {
         while(num-- > 1){
             int temp = queue1.peek();
             queue1.poll();
             queue2.offer(temp);
         }
         int ans = queue1.peek();
         queue1.poll();
         queue2.offer(ans);
         while(!queue2.isEmpty()){ // 把元素都运回来
             int temp = queue2.peek();
             queue2.poll();
             queue1.offer(temp);
             num++;
         }
         return ans;
     }
     
     public boolean empty() {
         return queue2.isEmpty() && queue1.isEmpty();
     }
 }

20.有效的括号

栈的简单应用,括号匹配

 class Solution {
     public boolean isValid(String s) {
         char[] record = s.toCharArray();
         Stack<Character> stack = new Stack<>();
         for(char c : record){
             if(c == '{' || c == '[' || c == '('){
                 stack.push(c);
             }
             else{
                 if(stack.isEmpty()) return false;
                 else 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;
     }
 }

1047.删除字符串中的所有相邻重复项

使用栈进行匹配的经典题目,值得记忆的是StringBuilder.insert()方法。

 class Solution {
     public String removeDuplicates(String s) {
         StringBuilder stringBuilder = new StringBuilder();
         char[] record = s.toCharArray();
         Stack<Character> stack = new Stack<>();
         for(char c : record){
             if(!stack.isEmpty() && c == stack.peek()) stack.pop();
             else stack.push(c);
         }
         while(!stack.isEmpty()){
             char temp = stack.peek();
             stringBuilder.insert(0,temp);
             stack.pop();
         }
         return stringBuilder.toString();
     }
 }