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

232. 用栈实现队列

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

代码随想录讲解:

由于java的栈和队列的底层实现忘得差不多了,所以这一遍直接看题解视频,把饭喂在嘴里,不思考了。

java有自己的栈结构,stack,那么在初始化MyQueue实体对象时,要初始化所有的属性,这种自定义的class类,属性值就放实现下列函数可能用到的所有东西就好,比如这里需要两个栈(一个入栈,一个出栈)。

push( ):直接往stackIn中push就可以了。

pop( ):队列的特征是先进先出,栈的特征是先进后出,所以要实现从队列中弹出元素时,保证其他元素还存在,就要借助另外一个栈,即分为入栈(in)和出栈(out),注意要在有弹出动作时把所有的元素都从入栈放到出栈中,否则顺序就乱了。如果stackOut为空,就要把stakIn中所有的元素都放到出栈中,由于出栈和查看队列首元素时都需要进行这个操作,所以集合成一个方法(dumpStackIn),在出栈为空时,将入栈中所有的元素移动到出栈中。

class MyQueue {
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;

    public MyQueue() {
        stackIn = new Stack<>();
        stackOut = new Stack<>();
    }
    
    public void push(int x) {
        stackIn.push(x);
    }
    
    public int pop() {
        dumpStackIn();
        return stackOut.pop();
    }
    
    public int peek() {
        dumpStackIn();
        return stackOut.peek();

    }
    
    public boolean empty() {
        dumpStackIn();
        if (stackOut.isEmpty()) return true;
        else return false;
    }

    public void dumpStackIn() {
        if (!stackOut.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.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. 用队列实现栈 - 力扣(LeetCode)

用队列来实现栈,同样考虑一下栈和队列的特征,栈先进后出,队列先进先出,不能像上道题一样使用两个队列来模拟,因为无法通过操作两个队列来实现顺序的反转(好像可以,就是将最后一个元素前面的所有元素存进第二个队列,然后将元素弹出)。

只用一个队列如何实现pop:将前面的元素先弹出,再放进队列。

java中有Queue和Deque两种数据结构(二刷补充)

class MyStack {
    Queue<Integer> queue;


    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.add(x);
    }
    
    public int pop() {
        dropQueue();
        return queue.poll();
    }
    
    public int top() {
        dropQueue();
        int result = queue.poll();
        queue.add(result);
        return result;
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }

    public void dropQueue() {
        int size = queue.size();
        for(int i = 0; i < size - 1; i++) {
            queue.add(queue.poll());
        }
    }
}

/**
 * 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. 有效的括号 - 力扣(LeetCode)

虽然能写,但是写出来一段极度不优雅的代码

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        if (s.length() == 1) return false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // if ((c == ')' && stack.peek() == '(') || (c == ']' && stack.peek() == '[') || (c == '}' && stack.peek() == '{')) {
            //     stack.pop();
            // } else {
            //     stack.push(c);
            // }
            if (c == ')') {
                if (!stack.isEmpty()) {
                    if (stack.peek() == '(') {
                        stack.pop();
                        continue;
                    }
                } 
                stack.push(c);
            }else if (c == ']') {
                if (!stack.isEmpty()) {
                    if (stack.peek() == '[') {
                        stack.pop();
                        continue;
                    } 
                }
                stack.push(c);
            }else if (c  == '}') {
                if (!stack.isEmpty()) {
                    if (stack.peek() == '{') {
                        stack.pop();
                        continue;
                    } 
                }
                stack.push(c);
            } else {
                stack.push(c);
            }
        }
        if (stack.isEmpty()) return true;
        return false;
    }
}

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

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

难度不高,学习一下StringBuffer的操作

class Solution {
    public String removeDuplicates(String s) {
        StringBuffer sb = new StringBuffer();
        int top = -1;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (top >= 0 && sb.charAt(top) == c) {
                sb.deleteCharAt(top);
                top--;
            } else {
                sb.append(c);
                top++;
            }
        }
        return sb.toString();
    }
}

day10,迟到的打卡,明天继续~

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值