二刷hot100-155.最小栈

📅 2026/8/20 15:47:05
二刷hot100-155.最小栈
巧妙栈中存入数组长度为2第一个为元素本身第二个为此时栈的最小值在push时比较当前元素和当前最小值就能得到最新的最小值class MinStack { Stackint[] s new Stack(); public MinStack() { s.push(new int[]{0,Integer.MAX_VALUE}); } public void push(int val) { s.push(new int[]{val,Math.min(getMin(),val)}); } public void pop() { s.pop(); } public int top() { return s.peek()[0]; } public int getMin() { return s.peek()[1]; } } /** * Your MinStack object will be instantiated and called as such: * MinStack obj new MinStack(); * obj.push(val); * obj.pop(); * int param_3 obj.top(); * int param_4 obj.getMin(); */