class Solution
{
public:
void push(int node) {
}
int pop() {
}
private:
stack<int> stack1;
stack<int> stack2;
};
栈1读入数字,然后换到栈2,注意当栈2没有数据的时候才从栈1读入数据
AC代码:
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop()
{
if(!stack2.empty()) {
int a = stack2.top();
stack2.pop();
return a;
}
else {
while(!stack1.empty()) {
int k = stack1.top();
stack2.push(k);
stack1.pop();
}
int a = stack2.top();
stack2.pop();
return a;
}
}
private:
stack<int> stack1;
stack<int> stack2;
};
原文:https://www.cnblogs.com/Hyouka/p/9256901.html