代码随想录 day 10 栈与队列

第五章 栈与队列part01

理论基础

了解一下 栈与队列的内部实现机制,文中是以C++为例讲解的。
文章讲解:https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

232.用栈实现队列

大家可以先看视频,了解一下模拟的过程,然后写代码会轻松很多。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html

225. 用队列实现栈

可能大家惯性思维,以为还要两个队列来模拟栈,其实只用一个队列就可以模拟栈了。
建议大家掌握一个队列的方法,更简单一些,可以先看视频讲解
题目链接/文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html

20. 有效的括号

讲完了栈实现队列,队列实现栈,接下来就是栈的经典应用了。
大家先自己思考一下 有哪些不匹配的场景,在看视频 我讲的都有哪些场景,落实到代码其实就容易很多了。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0020.%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7.html

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

栈的经典应用。
要知道栈为什么适合做这种类似于爱消除的操作,因为栈帮助我们记录了 遍历数组当前元素时候,前一个元素是什么。
题目链接/文章讲解/视频讲解:https://programmercarl.com/1047.%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.html

232.用栈实现队列

题目链接

https://leetcode.cn/problems/implement-queue-using-stacks/description/

解题思路

输入栈和输出栈,做一个模拟的过程。

code

class MyQueue {
    Stack<Integer> in=new Stack<>();
    Stack<Integer> out =new Stack<>();
    public MyQueue() {

    }
    
    public void push(int x) {
        in.push(x);
    }
    
    public int pop() {
        if(!out.isEmpty()){
            return out.pop();
        }
        while(!in.isEmpty()){
            out.push(in.pop());
        }
        return out.pop();
    }
    
    public int peek() {
        if(!out.isEmpty()){
            return out.peek();
        }
        while(!in.isEmpty()){
            out.push(in.pop());
        }
        return out.peek();
    }
    
    public boolean empty() {
        return in.isEmpty()&&out.isEmpty();
    }
    //抽象一个方法 复用
    private void inToOut(){
        if(!out.isEmpty()){
            return;
        }
        while(!in.isEmpty()){
            out.push(in.pop());
        }
    }
}

225. 用队列实现栈

题目链接

https://leetcode.cn/problems/implement-stack-using-queues/description/

解题思路

俩个队列,一个队列传输到另一个队列的最后一个元素就是要弹出的元素。

code

class MyStack {
    Queue<Integer> q1=new LinkedList<>();
    Queue<Integer> q2=new LinkedList<>();
    public MyStack() {

    }
    
    public void push(int x) {
        if(!q2.isEmpty()){
            q2.offer(x);
        }else{
            q1.offer(x);
        }
    }
    
    public int pop() {
        int res=-1;
        if(!q2.isEmpty()){
            while(!q2.isEmpty()){
                int pop=q2.poll();
                if(q2.isEmpty()){
                    res=pop;
                }else{
                    q1.offer(pop);
                }
                
            }
        }else{
            while(!q1.isEmpty()){
                int pop=q1.poll();
                if(q1.isEmpty()){
                     res=pop;
                }else{
                    q2.offer(pop);
                }
                
            }
        }
        return res;
    }
    
    public int top() {
        int top=this.pop();
        this.push(top);
        return top;
    }
    
    public boolean empty() {
        return q1.isEmpty()&&q2.isEmpty();
    }
}

20. 有效的括号

题目链接

https://leetcode.cn/problems/implement-stack-using-queues/description/

解题思路

总是比较相邻的元素,要考虑不匹配的情况有哪些,这样才能写出正确的代码
1.括号数量不是偶数
2.(( 判断俩元素不等
3.)( 当前元素一定是右括号才能和前面的匹配
最后判断栈是否空就是结果

code

public boolean isValid(String s) {
        if(s==null || s.length() %2 !=0){
            return false;
        }
        Stack<Character> stack=new Stack<>();
        char[] charArray=s.toCharArray();
        for(char c:charArray){
            if(!stack.isEmpty()){
                char peek=stack.peek();
                if(c!= peek && this.getBracket(c) == peek){
                    stack.pop();
                    continue;
                }
            }
            stack.push(c);
        }
        return stack.isEmpty();
    }
    private char getBracket(char c){
            if(c==']'){
                return '[';
            }else if(c==')'){
                return '(';
            }else if(c=='}'){
                return '{';
            }else{
                return c;
            }
    }

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

题目链接

https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/

解题思路

放入栈中,相邻元素相同则弹出栈中元素
最后取出栈中元素反转字符串

code

    public String removeDuplicates(String s) {
        Stack<Character> stack=new Stack<>();
        char[] charArray=s.toCharArray();
        for(char c: charArray){
            if(!stack.isEmpty()&&c==stack.peek()){
                stack.pop();
            }else{
                stack.push(c);
            }
        }
        StringBuilder sb=new StringBuilder();
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        reverseStr(sb,0,sb.length()-1);
        return sb.toString();
    }

    public void reverseStr(StringBuilder sb,int start,int end){
        while(start<end){
            char temp=sb.charAt(start);
            sb.setCharAt(start,sb.charAt(end));
            sb.setCharAt(end,temp);
            start++;
            end--;
        }
    }
  • 12
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值