最小栈
运算代码如下:
class MinStack {public: /** initialize your data structure here. */ MinStack() {
}
void push(int x) {
m_stack.push(x);
if(m_stack_min.empty() || x <= m_stack_min.top()) { m_stack_min.push(x);
}
}
void pop() {
if(m_stack.top() == m_stack_min.top())
{ m_stack_min.pop();
}
m_stack.pop();
}
int top()
{ return m_stack.top();
}
int getMin()
{ return m_stack_min.top();
}
private: stack<int> m_stack;
stack<int> m_stack_min;
};
此代码易理解