LeetCode-栈与队列

Posted by 黑矮皮卡 on October 28, 2024

栈实现队列

//入队栈和出队栈
class MyQueue {
Deque <Integer>inStack;
Deque <Integer>outStack;
    public MyQueue() {
        inStack=new LinkedList<Integer>();
        outStack=new LinkedList<Integer>();
    }
    public void push(int x) {
        inStack.push(x);
    }
    public int pop() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.pop();
    }
    public int peek() {
        if(outStack.isEmpty()){
            in2out();
        }
        return outStack.peek();
    }
    public boolean empty() {
        return inStack.isEmpty()&&outStack.isEmpty();
    }
    private void in2out(){
        while(!inStack.isEmpty()){
            outStack.push(inStack.pop());
        }
    }
}

队列实现栈

//主队列和辅助队列
class MyStack {
Deque<Integer>in1;
Deque<Integer>in2;
    public MyStack() {
        in1=new LinkedList<Integer>();
        in2=new LinkedList<Integer>();
    }    
    public void push(int x) {
        in2.offer(x);
        while(!in1.isEmpty()){
            in2.offer(in1.poll());
        }
        Deque<Integer>tem=in1;
        in1=in2;
        in2=tem;
    }
    public int pop() {
        return in1.poll();
    }
    
    public int top() {
        return in1.peek();
    }
    public boolean empty() {
        return in1.isEmpty();
    }
}

有效的括号

class Solution {
    public boolean isValid(String s) {
    if(s.length()%2!=0){
        return false;
    }
    Map<Character,Character>mp=new HashMap<Character,Character>();{
    mp.put(')','(');mp.put('}','{');mp.put(']','[');
    }
    Deque<Character>l=new ArrayDeque<>();
    for(char c:s.toCharArray()){
    if(!mp.containsKey(c)){//c是左括号
        l.push(c);
    }
    else if(l.isEmpty()||l.pop()!=mp.get(c)){//c是右括号
        return false;
    }
    }
    return l.isEmpty();
    }
}

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

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

逆波兰表达式求值

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer>st=new LinkedList<Integer>();
        int n=tokens.length;
        for(int i=0;i<n;i++){
            String token=tokens[i];
            if(isNumber(token)){
                st.push(Integer.parseInt(token));
            }else{
                int n2=st.pop();
                int n1=st.pop();
                switch(token){
                    case "+":
                    st.push(n1+n2);
                    break;
                    case "-":
                    st.push(n1-n2);
                    break;
                    case "*":
                    st.push(n1*n2);
                    break;
                    case "/":
                    st.push(n1/n2);
                    break;
                    default:
                }
            }  
        }
        return st.pop();
    }
    public boolean isNumber(String token){
        return!("+".equals(token)||"-".equals(token)||"*".equals(token)||"/".equals(token));
    }
}

滑动窗口最大值(Todo)

前k个高频元素(Todo)