代码随想录Day9

代码随想录Day9

第一题 232.用栈实现队列

  • 题目链接:力扣题目链接

  • 使用栈实现队列的下列操作:

    push(x) – 将一个元素放入队列的尾部。
    pop() – 从队列首部移除元素。
    peek() – 返回队列首部的元素。
    empty() – 返回队列是否为空。

  • AC代码

class MyQueue {

    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    /** Initialize your data structure here. */
    public MyQueue() {
        stackIn = new Stack<>(); // 负责进栈
        stackOut = new Stack<>(); // 负责出栈
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        stackIn.push(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        dumpstackIn();
        return stackOut.pop();
    }

    /** Get the front element. */
    public int peek() {
        dumpstackIn();
        return stackOut.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stackIn.isEmpty() && stackOut.isEmpty();
    }

    // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
    private void dumpstackIn(){
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()){
            stackOut.push(stackIn.pop());
        }
    }
}

第二题 225. 用队列实现栈

  • 题目链接:力扣题目链接(opens new window)

    使用队列实现栈的下列操作:

    • push(x) – 元素 x 入栈
    • pop() – 移除栈顶元素
    • top() – 获取栈顶元素
    • empty() – 返回栈是否为空
  • AC代码

class MyStack {
    Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    public void push(int x) {
        queue.add(x);
    }

    public int pop() {
        rePosition();
        return queue.poll();
    }

    public int top() {
        rePosition();
        int result = queue.poll();
        queue.add(result);
        return result;
    }

    public boolean empty() {
        return queue.isEmpty();
    }

    public void rePosition(){
        int size = queue.size();
        size--;
        while(size-->0)
            queue.add(queue.poll());
    }
}

第三题 20. 有效的括号

  • 题目链接:力扣题目链接

  • 给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串,判断字符串是否有效。

    有效字符串需满足:

    • 左括号必须用相同类型的右括号闭合。
    • 左括号必须以正确的顺序闭合。
    • 注意空字符串可被认为是有效字符串。

    示例 1:

    • 输入: “()”
    • 输出: true

    示例 2:

    • 输入: “()[]{}”
    • 输出: true

    示例 3:

    • 输入: “(]”
    • 输出: false

    示例 4:

    • 输入: “([)]”
    • 输出: false

    示例 5:

    • 输入: “{[]}”
    • 输出: true
  • 题目思路:

    • 这道题目的关键是需要先理清楚三种不符合条件的情况
      1. 左括号数量大于 右括号
      2. 右括号数量大于左括号
      3. 左右括号数量相等但是类型不匹配
    • 这道题目还有一个比较巧妙的思路是在遍历的左括号时间入栈的是其对应的右括号
  • AC代码

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //遍历传入的字符串,处理不匹配的情况
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') stack.push(')');
            else if (s.charAt(i) == '[') stack.push(']');
            else if (s.charAt(i) == '{') stack.push('}');
                //第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false
                // 第二种情况:遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
            else if (stack.empty() || s.charAt(i) != stack.peek()) return false;
            else stack.pop();
        }
        // 第一种情况:此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true
        return stack.empty();
    }
}

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

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

  • 给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。

    在 S 上反复执行重复项删除操作,直到无法继续删除。

    在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

    示例:

    输入:“abbaca”
    输出:“ca”
    解释:例如,在 “abbaca” 中,我们可以删除 “bb” 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 “aaca”,其中又只有 “aa” 可以执行重复项删除操作,所以最后的字符串为 “ca”。
    提示:

    1 <= S.length <= 20000
    S 仅由小写英文字母组成。

  • 题目思路:这道题目的思路也比较简单和匹配括号差不多,通过栈结构来进行匹配,在遍历过程中遇到和栈中相同的就弹出否则入栈,最后输出栈中元素,这里需要反转一下最后的字符串因为栈的输出是反的

  • AC代码

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (stack.empty()||s.charAt(i) != stack.peek()) stack.push(s.charAt(i));
            else stack.pop();
        }
        String result = "";
        while (!stack.empty()) {
            result += stack.peek();
            stack.pop();
        }
        return new StringBuilder(result).reverse().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值