private Stack<Integer> stack = new Stack<>(); private Stack<Integer> minStack = new Stack<>(); public(){ } publicvoidpush(int x){ if (minStack.isEmpty() || x <= minStack.peek()) { minStack.push(x); } stack.push(x); } publicvoidpop(){ if (stack.peek().equals(minStack.peek())) { minStack.pop(); } stack.pop(); } publicinttop(){ return stack.peek(); } publicintgetMin(){ return minStack.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.getMin(); */
近期评论