简介
使用系统库
code
class MinStack {
public:
/** initialize your data structure here. */
deque<int> s;
MinStack() {
}
void push(int val) {
s.push_back(val);
}
void pop() {
s.pop_back();
}
int top() {
return s.back();
}
int getMin() {
int minn = s[0];
for(auto it: s){
minn = min(it, minn);
}
return minn;
}
};
class MinStack {
Deque<Integer> s;
/** initialize your data structure here. */
public MinStack() {
s = new LinkedList<Integer>();
}
public void push(int val) {
s.push(val);
}
public void pop() {
s.pop();
}
public int top() {
return s.peek(); // 顶部竟然返回的是peek, 还是C++直观
}
public int getMin() {
int imin = Integer.MAX_VALUE;
for(Integer it:s){
imin = Math.min(it, imin);
}
return imin;
}
}
/**
* 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();
*/
---------------------------我的天空里没有太阳,总是黑夜,但并不暗,因为有东西代替了太阳。虽然没有太阳那么明亮,但对我来说已经足够。凭借着这份光,我便能把黑夜当成白天。我从来就没有太阳,所以不怕失去。
--------《白夜行》