225. Implement Stack using Queues 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556class MyStack {private: queue<int> q;public: MyStack() { } void push(int x) { q.push(x); } int pop() { queue<int> temp; int ret ; int size = q.size(); size--; while(size--){ temp.push(q.front()); q.pop(); } ret = q.front(); q.pop(); while(!temp.empty()){ q.push(temp.front()); temp.pop(); } return ret; } int top() { queue<int> temp; int ret ; int size = q.size(); while(size--){ ret = q.front(); temp.push(q.front()); q.pop(); } while(!temp.empty()){ q.push(temp.front()); temp.pop(); } return ret; } bool empty() { return q.empty(); }}; option 112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758class MyStack {private: queue<int> q1, q2;public: MyStack() { } void push(int x) { if(empty()) q1.push(x); else{ if(q1.size()>0){ q2.push(x); int size = q1.size(); while(size >0){ q2.push(q1.front()); q1.pop(); size--; } } else if(q2.size() > 0){ q1.push(x); int size = q2.size(); while(size>0){ q1.push(q2.front()); q2.pop(); size--; } } } } int pop() { if(q1.size()>0) { int t = q1.front(); q1.pop(); return t; } else if(q2.size()>0) { int t = q2.front(); q2.pop(); return t; } return 0; } int top() { if(q1.size()>0) return q1.front(); else if(q2.size()>0) return q2.front(); return 0; } bool empty() { return q1.empty() && q2.empty(); }}; analysis pop and top operation time complexity O(n) space complexity O(n)