讨论/《图解算法数据结构》 - 剑指 Offer 30 题目解析/
《图解算法数据结构》 - 剑指 Offer 30 题目解析

class MinStack {

LinkedList<Integer> A, B;

/** initialize your data structure here. */
public MinStack() {
    A = new LinkedList<>();
    B = new LinkedList<>();
}

public void push(int x) {
    A.addFirst(x);
    if (B.isEmpty() || B.getFirst() >= x) {
        B.addFirst(x);
    }
}

public void pop() {
    if (A.removeFirst().equals(B.getFirst())) {
        B.removeFirst();
    }
}

public int top() {
    return A.getFirst();
}

public int min() {
    return B.getFirst();
}

}

/**

  • 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();
    */
展开全部 9 讨论