代码随想录Da11 | 232. 用栈实现队列 、225. 用队列实现栈 、20. 有效的括号、1047. 删除字符串中的所有相邻重复项 | 栈

71 阅读1分钟

232. 用栈实现队列

题目链接:232. 用栈实现队列

思路: 用两个栈,使用 peek 或 pop 操作队头的元素时,若 s2 为空,可以把 s1 的所有元素取出再添加进 s2这时候 s2 中元素就是先进先出顺序了

我的代码:

class MyQueue {

    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    
    public void push(int x) {
        s1.push(x);
    }
    
    public int pop() {
        // check();
        // return s2.pop();
        if (s2.isEmpty()) {
            while (!s1.isEmpty()) s2.push(s1.pop());
        }
        return s2.pop();
    }
    
    public int peek() {
        // check();
        // return s2.peek();
        if (s2.isEmpty()) {
            while (!s1.isEmpty()) s2.push(s1.pop());
        }
        return s2.peek();
    }
    
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }

    public void check() {
        if (!s2.isEmpty()) return;
        while (!s1.isEmpty()) {
            s2.push(s1.pop());
        }
    }
}

/**
 * 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. 用队列实现栈

题目链接:225. 用队列实现栈

思路: 把队尾元素前面的所有元素重新塞到队尾,让队尾元素排到队头,这样就可以取出了。

我的代码:

class MyStack {

    Queue<Integer> q;
    int top = 0;

    public MyStack() {
        q = new LinkedList<>();
    }
    
    public void push(int x) {
        q.offer(x);
        top = x;
    }
    
    public int pop() {
        int size = q.size();
        while (size > 2) {
            q.offer(q.poll());
            size--;
        }
        top = q.peek();
        q.offer(q.poll());
        return q.poll();
    }
    
    public int top() {
        return top;
    }
    
    public boolean empty() {
        return q.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

问题:

总结:

20. 有效的括号

题目链接:20. 有效的括号

思路: 遇到左括号就入栈,遇到右括号就去栈中寻找最近的左括号,看是否匹配。

我的代码:

class Solution {
    public boolean isValid(String s) {
        Stack<Character> s1 = new Stack<>();
        char[] ch = s.toCharArray();
        for(char c : ch) {
            if (c == '(' || c == '[' || c == '{') s1.push(c); 
            else if (!s1.isEmpty() && c == ')' && s1.peek() == '(') s1.pop(); 
            else if (!s1.isEmpty() && c == ']' && s1.peek() == '[') s1.pop();
            else if (!s1.isEmpty() && c == '}' && s1.peek() == '{') s1.pop();
            else return false;
        }
        return s1.isEmpty();
        
    }
}

问题:

总结:

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

题目链接:1047. 删除字符串中的所有相邻重复项

思路:

使用栈即可删除相邻重复的字母,最后把栈中剩余字母弹出到stringbuilder,在进行反转。

我的代码:

 class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        char[] ch = s.toCharArray();
        for (char c : ch) {
            if (stack.isEmpty() || stack.peek() != c) stack.push(c);
            else if (stack.peek() == c) stack.pop();
        }

        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }

        return sb.reverse().toString();
    }
}

问题:

总结: