232.用栈实现队列
题目链接:232.用栈实现队列
文档链接:232.用栈实现队列
视频链接:栈的基本操作! | LeetCode:232.用栈实现队列
C++实现
class MyQueue {
public:
stack<int> stackin;
stack<int> stackout;
MyQueue() {
}
void push(int x) {
stackin.push(x);
}
int pop() {
if(stackout.empty()){
while(!stackin.empty()){
stackout.push(stackin.top());
stackin.pop();
}
}
int result = stackout.top();
stackout.pop();
return result;
}
int peek() {
int result = this->pop();
stackout.push(result);
return result;
}
bool empty() {
return stackin.empty()&&stackout.empty();
}
};
225. 用队列实现栈
题目链接:225. 用队列实现栈
文档链接:225. 用队列实现栈
视频链接:队列的基本操作! | LeetCode:225. 用队列实现栈
C++实现
class MyStack {
public:
queue<int> que;
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int s = que.size();
s--;
while(s--){
que.push(que.front());
que.pop();
}
int result = que.front();
que.pop();
return result;
}
int top() {
return que.back();
}
bool empty() {
return que.empty();
}
};