先上链接 https://www.cnblogs.com/loveyixiang/p/6030920.html
邮个疑问: 就是当 B 栈的数据先出完了,再怎么获取最小值呢?
邮个疑问: 就是当 B 栈的数据先出完了,再怎么获取最小值呢?

1 ai277014717 Feb 28, 2018 B 不会出完,保证 B 跟 A 栈元素个数一样。当初的面试题啊,一脸蒙蔽 |
2 ai277014717 Feb 28, 2018 `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 Feb 28, 2018 ``` 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 |