2020-10-08 queue and stack

    科技2025-05-28  7

    leetcode stack and queue

    10-08my queuemy stack stack and queue

    10-08

    my queue

    class MyQueue { public: stack<int> input,output; /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { input.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { int a = peek(); output.pop(); return a; } /** Get the front element. */ int peek() { if(output.empty()){ while(input.size()){ output.push(input.top()); input.pop(); } } return output.top(); } /** Returns whether the queue is empty. */ bool empty() { return input.empty() && output.empty(); } };

    my stack

    Processed: 0.013, SQL: 8