题目
代码分析
多使用一个栈即可
代码实现
import java.util.*; class MinStack { public Stack<Integer> ordStack; public Stack<Integer> minStack; public MinStack() { ordStack=new Stack<>(); minStack=new Stack<>(); } public void push(int x) { ordStack.push(x); if(!minStack.isEmpty()) { int temp=minStack.peek(); if(x>temp) { minStack.push(temp); }else { minStack.push(x); } }else { minStack.push(x); } } public void pop() { ordStack.pop(); minStack.pop(); } public int top() { return ordStack.peek(); } public int getMin() { return minStack.peek(); } }
学习情况
1次