首页 > 编程语言 >代码随想录算法训练营第10天 | 复习队列和栈

代码随想录算法训练营第10天 | 复习队列和栈

时间:2024-07-12 15:09:07浏览次数:15  
标签:10 obj int s2 训练营 随想录 pop public size

2024年7月12日

题232. 用栈实现队列

两边倒即可,要出队列就倒到右边去,然后再回来。

class MyQueue {

    Stack<Integer> s1;
    Stack<Integer> s2;
    int size;

    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
        size= 0;
    }   
    
    public void push(int x) {
        s1.push(x);
        size+=1;
    }
    
    public int pop() {
        while(!s1.isEmpty()){
            s2.push(s1.pop());
        }
        int res =  s2.pop();
         while(!s2.isEmpty()){
            s1.push(s2.pop());
        }
        size-=1;
        return res;
    }
    
    public int peek() {
        while(!s1.isEmpty()){
            s2.push(s1.pop());
        }
        int res =  s2.peek();
         while(!s2.isEmpty()){
            s1.push(s2.pop());
        }
        return res;
    }
    
    public boolean empty() {
        return size==0;
    }
}

/**
 * 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. 用队列实现栈
复习了Java中Queue的实现类LinkedList,可以用offer从队尾添加元素,poll头部取出元素。

class MyStack {

    Queue<Integer> q1;
    Queue<Integer> q2;
    int size;

    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
        size=0;
    }
    
    public void push(int x) {
        q1.offer(x);
        size+=1;
    }
    
    public int pop() {
        int n = size;
        while(n>1){
            q2.offer(q1.poll());
            n-=1;
        }
        int res = q1.poll();
        size-=1;
        while(!q2.isEmpty()){
            q1.offer(q2.poll());
        }
        return res;
    }
    
    public int top() {
        int n = size;;
        while(n>1){
            q2.offer(q1.poll());
            n-=1;
        }
        int res = q1.poll();
        q2.offer(res);
        while(!q2.isEmpty()){
            q1.offer(q2.poll());
        }
        return res;
    }
    
    public boolean empty() {
        return size==0;
    }
}

/**
 * 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. 有效的括号
用栈即可,要注意pop如果是空栈会直接报错,所以pop前要做好判断。

class Solution {
    public boolean isValid(String s) {
        Stack<Character> st = new Stack<>();
        for(int i=0;i<s.length();i++){
            char x = s.charAt(i);
            if((x=='(')||(x=='{')||(x=='[')){
                st.push(x);
            }
            if(x==')'){
                if(st.isEmpty()){
                    return false;
                }
                char xx = st.pop();
                if(xx!='('){
                    return false;
                }
            }
            if(x=='}'){
                if(st.isEmpty()){
                    return false;
                }
                char xx = st.pop();
                if(xx!='{'){
                    return false;
                }
            }
            if(x==']'){
                if(st.isEmpty()){
                    return false;
                }
                char xx = st.pop();
                if(xx!='['){
                    return false;
                }
            }
        }
        if(!st.isEmpty()){
            return false;
        }
        return true;
    }
}

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

用栈,如果新来的char和栈顶相同,就把栈顶的去掉,否则入栈。
错误还是在忘了判断栈为空就pop,这样会直接报错。

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if(stack.isEmpty()){
                stack.push(c);
                continue;
            }
            if(top(stack)==c){
                stack.pop();
                continue;
            }else{
                stack.push(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while(!stack.isEmpty()){
            sb.append(stack.pop());
        }
        sb = sb.reverse();
        return new String(sb);
    }

    public Character top(Stack<Character> stack){

        char c = stack.pop();
        stack.push(c);
        return c;
    }
}

标签:10,obj,int,s2,训练营,随想录,pop,public,size
From: https://www.cnblogs.com/hailicy/p/18298439

相关文章