letcode 分类练习 栈和队列 232.用栈实现队列 225. 用队列实现栈 20. 有效的括号 1047. 删除字符串中的所有相邻重复项
232.用栈实现队列
两个栈,一个栈负责输入,一个栈负责输出,如果输出栈空了就从输入栈里面导入
class MyQueue {
public:
stack<int> s1;
stack<int> s2;
MyQueue() {
}
void push(int x) {
s1.push(x);
}
int pop() {
if(s2.empty()){
while(!s1.empty()){
int val = s1.top();
s1.pop();
s2.push(val);
}
}
int x = s2.top();
s2.pop();
return x;
}
int peek() {
if(s2.empty()){
while(!s1.empty()){
int val = s1.top()