2024-03-04 2024-08-05 problem 1 分钟读完 (大约144个字) 0次访问lc232.用栈实现队列题目链接: 题解 简单模拟 参考代码 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465/* * @lc app=leetcode.cn id=232 lang=cpp * * [232] 用栈实现队列 */// @lc code=startclass MyQueue { stack<int>s1; stack<int>s2;public: MyQueue() { } void push(int x) { s1.push(x); } int pop() { while(!s1.empty()) { s2.push(s1.top()); s1.pop(); } int ret = s2.top(); s2.pop(); while(!s2.empty()) { s1.push(s2.top()); s2.pop(); } return ret; } int peek() { while(!s1.empty()) { s2.push(s1.top()); s1.pop(); } int ret = s2.top(); // s2.pop(); while(!s2.empty()) { s1.push(s2.top()); s2.pop(); } return ret; } bool empty() { if(s1.empty()) return true; else return false; }};/** * 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(); */// @lc code=end lc232.用栈实现队列https://blog.xiang578.com/problem/lc232.html作者Ryen Xiang发布于2024-03-04更新于2024-08-05许可协议 LeetCode