剑指 Offer 09.30. 栈

题目1:

20. 有效的括号https://leetcode-cn.com/problems/valid-parentheses/

代码:

class Solution {
    public static final HashMap<Character,Character> map = new HashMap<>(){{
        put(')','(');
        put(']','[');
        put('}','{');
    }};
    public boolean isValid(String s) {
        Stack<Character> st = new Stack<>();
        for(int i=0;i<s.length();i++){
            if(!st.empty()){
                if(isMatchingMap(st.peek(),s.charAt(i))){
                    st.pop();
                    continue;
                }
            }
            st.push(s.charAt(i));
        }
        return st.empty();
    }
    boolean isMatching(char a,char b){
        //根据ascii码判断
        if(a+1==b||a+2==b)
            return true;
        else
            return false;
    }
    boolean isMatchingMap(Character a,Character b){
        if(map.get(b)==a)
            return true;
        else
            return false;
    }
}

结果:

题目2:

剑指 Offer 09. 用两个栈实现队列https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/

 代码:

class CQueue {
    Stack<Integer> st1;
    Stack<Integer> st2;
    public CQueue() {
        st1=new Stack<Integer>();
        st2=new Stack<Integer>();
    }
    
    public void appendTail(int value) {
        st1.push(value);
    }
    
    public int deleteHead() {
        if(st2.empty()){
            //如果st2是空的 则将s1里有的倒进来
            while(!st1.empty())
                st2.push(st1.pop());
        }
        if(!st2.empty())//如果倒完2非空 则pop
            return st2.pop();
        else//如果倒完2还是空的 则返回-1
            return -1;
    }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */

结果:

题目3:

 剑指 Offer 30. 包含min函数的栈https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/

 代码一:

class MinStack {
    Stack<Integer> st1;
    Stack<Integer> st2;
    int min=(int)-Math.pow(2,31)-1;
    /** initialize your data structure here. */
    public MinStack() {
        st1=new Stack<Integer>();
        st2=new Stack<Integer>();
    }
    
    public void push(int x) {
        //用st2同步维护最小值栈
        min=min<x?min:x;
        st1.push(x);
        st2.push(min);
    }
    
    public void pop() {
        st1.pop();
        st2.pop();
        //记得弹栈时同步更新最小值
        if(!st2.empty())
            min=st2.peek();
        else
            min=(int)-Math.pow(2,31)-1;
    }
    
    public int top() {
        return st1.peek();
    }
    
    public int min() {
        return st2.peek();
    }
}
/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */

代码二:参考他人 不通过min更新最小值

class MinStack {
    Stack<Integer> A, B;
    public MinStack() {
        A = new Stack<>();
        B = new Stack<>();
    }
    public void push(int x) {
        A.push(x);
        if(B.empty() || B.peek() >= x)
            B.push(x);
    }
    public void pop() {
        if(A.pop().equals(B.peek()))
            B.pop();
    }
    public int top() {
        return A.peek();
    }
    public int min() {
        return B.peek();
    }
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值