C++版本:
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
int a=stack2.top();
stack2.pop();
while(!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
return a;
}
private:
stack<int> stack1;
stack<int> stack2;
};
Python 版本:
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
#栈A用来入队列
#栈B用来出队列,当栈B为空时,栈A全部出栈到B,B在出栈(即出队列)
self.stackA=[]
self.stackB=[]
def push(self, node):
# write code here
self.stackA.append(node)
def pop(self):
# return xx
if self.stackB:
return self.stackB.pop()
elif not self.stackA:
return None
else:
while self.stackA:
self.stackB.append(self.stackA.pop())
return self.stackB.pop()