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

232.用栈实现队列

使用Stack

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() {
        if(!s2.empty()) return s2.pop();
        while(!s1.empty()){
            int e = s1.pop();
            s2.push(e);
        }
        return s2.pop();
    }
    
    public int peek() {
        if(!s2.empty()) return s2.peek();
        while(!s1.empty()){
            int e = s1.pop();
            s2.push(e);
        }
        return s2.peek();
    }
    
    public boolean empty() {
        return s1.empty() && s2.empty();
    }
}

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

其实一个队列就可以模拟栈。

class MyStack {
    private Deque<Integer> d1;
    private Deque<Integer> d2;
    private int count;

    public MyStack() {
        d1 = new ArrayDeque<>();
        d2 = new ArrayDeque<>();
        count = 0;
    }
    
    public void push(int x) {
        if(d2.isEmpty()) d1.offer(x);
        else d2.offer(x);
        count++;
    }
    
    public int pop() {
        Deque<Integer> d = d2.isEmpty() ? d1 : d2;
        Deque<Integer> dEmpty = d2.isEmpty() ? d2 : d1;

        for(int i=0; i<count-1; i++){
            dEmpty.offer(d.poll());
        }
        count--;
        return d.poll();
    }
    
    public int top() {
        Deque<Integer> d = d2.isEmpty() ? d1 : d2;
        Deque<Integer> dEmpty = d2.isEmpty() ? d2 : d1;

        for(int i=0; i<count-1; i++){
            dEmpty.offer(d.poll());
        }
        int e = d.poll();
        dEmpty.offer(e);
        return e;
    }
    
    public boolean empty() {
        return count==0;
    }
}

20. 有效的括号

我做的比较麻烦,在pop之前,首先要考虑是否为空。
事后想想,将pop放在if判断时,会导致判断一次就pop一次。但是这个代码可以通过,因为如果弹出的不符合,就直接return了。

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

优化:代码随想录解法

// 解法二
// 对应的另一半一定在栈顶
class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(char c : s.toCharArray()){
            // 有对应的另一半就直接消消乐
            if(c == ')' && !stack.isEmpty() && stack.peek() == '(')
                stack.pop();
            else if(c == '}' && !stack.isEmpty() && stack.peek() == '{')
                stack.pop();
            else if(c == ']' && !stack.isEmpty() && stack.peek() == '[')
                stack.pop();
            else
                stack.push(c);// 没有匹配的就放进去
        }

        return stack.isEmpty();
    }
}

最优解:将另一个括号push

class Solution {
    public boolean isValid(String s) {
        Deque<Character> deque = new LinkedList<>();
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch = s.charAt(i);
            //碰到左括号,就把相应的右括号入栈
            if (ch == '(') {
                deque.push(')');
            }else if (ch == '{') {
                deque.push('}');
            }else if (ch == '[') {
                deque.push(']');
            } else if (deque.isEmpty() || deque.peek() != ch) {
                return false;
            }else {//如果是右括号判断是否和栈顶元素匹配
                deque.pop();
            }
        }
        //遍历结束,如果栈为空,则括号全部匹配
        return deque.isEmpty();
    }
}

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

其实可以count计数,然后new一个数组。

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> st = new Stack<>();
        for(char c : s.toCharArray()){
            if(st.empty()) st.push(c);
            else{
                if(c==st.peek()) st.pop();
                else st.push(c);
            }
        }

        StringBuilder sb = new StringBuilder();
        while(!st.empty()){
            sb.append(st.pop());
        }
        return sb.reverse().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值