1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class MyQueue { public: stack<int> s1, s2; MyQueue() { } void push(int x) { s1.push(x); } int pop() { while(s1.size() > 1) s2.push(s1.top()), s1.pop(); int t = s1.top(); s1.pop(); while(s2.size()) s1.push(s2.top()), s2.pop(); return t; } int peek() { while(s1.size() > 1) s2.push(s1.top()), s1.pop(); int t = s1.top(); while(s2.size()) s1.push(s2.top()), s2.pop(); return t; } bool empty() { return s1.empty(); } };
|