题解 | #包含min函数的栈#
包含min函数的栈
http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
//使用两个栈实现min函数
class Solution {
public:
stack<int> S;
stack<int> S2;
void push(int value) {
S.push(value);
}
void pop() {
S.pop();
}
int top() {
return S.top();
}
int min() {
int bin=S.top();
while(S.size()>0){
if(bin>=S.top()){
bin=S.top();
}
S2.push(S.top());
S.pop();
}
while(S2.size()>0){
S.push(S2.top());
S2.pop();
}
return bin;
}
};