2024每日刷题(131)
Leetcode—232. 用栈实现队列
实现代码
class MyQueue {
public:MyQueue() {}void push(int x) {st.push(x);}int pop() {if(show.empty()) {if(empty()) {return -1;} else {int ans = show.top();show.pop();return ans;}} else {int ans = show.top();show.pop();return ans;}}int peek() {if(show.empty()) {if(empty()) {return -1;} else {return show.top();}} else {return show.top();}}bool empty() {if(show.empty()) {if(st.empty()) {return true;} else {while(!st.empty()) {show.push(st.top());st.pop();}return false;}} else {return false;}}
private:stack<int> st;stack<int> show;
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue* obj = new MyQueue();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->peek();* bool param_4 = obj->empty();*/
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!