注意栈的基本操作与vector略有不同~
class Solution
{
public:
void push(int node) {
stack1.push(node);//
}
int pop() {
int ele_top=0;
if(!stack2.empty()){//stack2不为空,就删除stack2的栈顶元素
ele_top=stack2.top();//top()返回栈顶元素,但不删除
stack2.pop();//pop()删除栈顶元素,但不返回该值
return ele_top;
}
else{//stack2为空,要把stack1中的元素依次弹出并压入stack2中
int ele_temp=0;
while(!stack1.empty()){
ele_temp=stack1.top();
stack2.push(ele_temp);
stack1.pop();
}
ele_top=stack2.top();//返回栈顶元素,但不删除
stack2.pop();//删除栈顶元素,但不返回该值
return ele_top;
}
}
private:
stack<int> stack1;
stack<int> stack2;
};
class Solution {
public:
void push(int node) { //元素压入stack1中
stack1.push(node);
return;
}
int pop() {
if(stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
int temp = stack2.top();
stack2.pop();
return temp;
}
private:
stack<int> stack1;
stack<int> stack2;
};