代码随想录刷题-栈与队列(上)

本文是每天跟着代码随想录刷题时做的笔记,用来总结与复习。

目录

232.用栈实现队列

225.用队列实现栈

 20.有效的括号

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

 总结


232.用栈实现队列

题目链接:232. 用栈实现队列 - 力扣(LeetCode) (leetcode-cn.com) 

思路:设置一个输入栈和一个输出栈

队列 push 操作:直接向输入栈里输入

队列 pop 、peek 操作:如果输出栈为空,就使用栈 pop 操作将输入栈的元素移动到输出栈,由于栈是先进后出,因此输出栈的元素顺序和之前输入栈的顺序相反,此时对输出栈执行对应 pop 、peek 操作就可得正确结果

队列 empty 操作:如果输入栈和输出栈中都没有元素,就表示队列为空

class MyQueue {
    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();
    public MyQueue() {

    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if (stack2.empty()){
            while (!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if (stack2.empty()){
            while (!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.empty() && stack2.empty();
    }
}

225.用队列实现栈

题目链接:225. 用队列实现栈 - 力扣(LeetCode) (leetcode-cn.com)

思路:

整体思路是用两个队列依次存储元素,详细如下:

empty 操作:如果两个队列都为空,则表示该栈为空

class MyStack {
    Queue<Integer> queue1 = new PriorityQueue<>();
    Queue<Integer> queue2 = new PriorityQueue<>();
    int size = 0;
    public MyStack() {

    }

    public void push(int x) {
        if (!queue1.isEmpty()){
            queue1.add(x);
        }else{
            queue2.add(x);
        }
        size++;
    }

    public int pop() {
        int ans = 0;
        if (queue2.isEmpty()){
            for (int i = 1; i < size; i++) {
                queue2.add(queue1.remove());
            }
            ans = queue1.remove();
            size--;
        }else if (queue1.isEmpty()){
            for (int i = 1; i < size; i++) {
                queue1.add(queue2.remove());
            }
            ans = queue2.remove();
            size--;
        }
        return ans;
    }

    public int top() {
        int ans = 0;
        if (queue2.isEmpty()){
            for (int i = 1; i < size; i++) {
                queue2.add(queue1.remove());
            }
            ans = queue1.remove();
            queue2.add(ans);
        }else if (queue1.isEmpty()){
            for (int i = 1; i < size; i++) {
                queue1.add(queue2.remove());
            }
            ans = queue2.remove();
            queue1.add(ans);
        }
        return ans;
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

 20.有效的括号

题目链接:20. 有效的括号 - 力扣(LeetCode) (leetcode-cn.com)

思路:用栈判断,遇见左括号则存入栈。遇见右括号,如果栈不为空,则判断与栈顶的左括号是否匹配,如果匹配则删除栈顶元素,如果不匹配,则表示字符串无效;如果栈为空则表示字符串无效。遍历完成后,如果栈为空,则表明该字符串有效。

public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Character,Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else {
                if (stack.empty() || map.get(ch) != stack.peek()){
                    return false;
                }else {
                    stack.pop();
                }
            }
        }
        if (stack.empty()){
            return true;
        }else {
            return false;
        }
    }

优化:思路差不多,只是在遇见左括号时,向栈中存入其对应右括号,遇见右括号,则判断是否与栈顶的右括号相同,这样就可以避免用一个 map 来存储括号对应关系

public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '('){
                stack.push(')');
            }else if (ch == '['){
                stack.push(']');
            }else if (ch == '{'){
                stack.push('}');
            }else {
                if (stack.empty() || stack.peek() != ch){
                    return false;
                }else {
                    stack.pop();
                }
            }
        }
        if (stack.empty()){
            return true;
        }else {
            return false;
        }
    }

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

题目链接:1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode) (leetcode-cn.com)

思路:使用栈思想,遍历字符串,如果与栈顶元素相同,则删除栈顶元素,不同则存入栈中

直接使用 Stack 类

用 StringBuilder 来模拟栈操作

 public String removeDuplicates(String s) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int len = str.length() - 1;
            char ch = s.charAt(i);
            if (str.length() == 0){
                str.append(ch);
            }else {
                if (ch == str.charAt(len)){
                    str.deleteCharAt(len);
                }else {
                    str.append(ch);
                }
            }
        }
        return new String(str);
    }

 

 总结

做的是四道简单题,就只是简单的运用了栈和队列先进后出、先进先出的特点,没有太大的思维难度。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值