class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { stk.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { while(stk.size() != 1) { int x = stk.top(); stk.pop(); helper.push(x); } int x = stk.top(); stk.pop(); while(helper.size()) { int x = helper.top(); helper.pop(); stk.push(x); } return x; } /** Get the front element. */ int peek() { while(stk.size() != 1) { int x = stk.top(); stk.pop(); helper.push(x); } int x = stk.top(); while(helper.size()) { int x = helper.top(); helper.pop(); stk.push(x); } return x; } /** Returns whether the queue is empty. */ bool empty() { return stk.size() == 0; } private: stack<int > stk, helper; };
Accepted
17/17 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 5.28 % of cpp submissions (9.1 MB)