1
ai277014717 2018-02-28 16:50:40 +08:00
B 不会出完,保证 B 跟 A 栈元素个数一样。当初的面试题啊,一脸蒙蔽
|
2
ai277014717 2018-02-28 17:11:26 +08:00
`class MinStack {
public: /** initialize your data structure here. */ stack<int> sta; stack<int> stb; MinStack() { } void push(int x) { sta.push(x); if(stb.size()==0){ stb.push(x); } else { if(stb.top()>x){ stb.push(x); } else { stb.push(stb.top()); } } } void pop() { sta.pop(); stb.pop(); } int top() { return sta.top(); } int getMin() { return stb.top(); } };` 刚刚水了一题 |
3
ai277014717 2018-02-28 17:18:36 +08:00
```
class MinStack { public: /** initialize your data structure here. */ stack<int> sta; stack<int> stb; MinStack() { } void push(int x) { sta.push(x); if(stb.size()==0){ stb.push(x); } else { if(stb.top()>x){ stb.push(x); } else { stb.push(stb.top()); } } } void pop() { sta.pop(); stb.pop(); } int top() { return sta.top(); } int getMin() { return stb.top(); } }; ``` 试试 markdown |