https://leetcode-cn.com/problems/implement-queue-using-stacks/ 问题描述 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty): 实现MyQueue 类: void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 int peek() 返回队列开头的...
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. You may assume that all operations are valid (for example, no pop or peek operations will be...
queue1 = queue2; queue2 = temp;returnoldTop; }/** Get the top element. */publicinttop(){returntop; }/** Returns whether the stack is empty. */publicbooleanempty(){returnqueue1.isEmpty(); } }
class Queue(object): def __init__(self): """ initialize your data structure here. """ self.inStack=[] self.outStack=[] def push(self, x): """ :type x: int :rtype: nothing """ self.inStack.append(x) def pop(self): """ :rtype: nothing """ self.peek() self.outStack....
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. You may assume that all operations are valid (for example, no pop or top operations will be...
来源:https://leetcode.com/problems/implement-queue-using-stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. ...
leetcode 232. Implement Queue using Stacks classMyQueue {public:/** Initialize your data structure here.*/stack<int>r, b; MyQueue() { }/** Push element x to the back of queue.*/voidpush(intx) { r.push(x); }/** Removes the element from in front of queue and returns that ...
232. Implement Queue using Stacks 1classMyQueue2{3private:4stack<int>st;5public:6/** Initialize your data structure here.*/7MyQueue() {89}1011/** Push element x to the back of queue.*/12voidpush(intx)13{14stack<int>cur;15if(!st.empty())16while(!st.empty())17{18inti=st.top...
Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty....
queue.peek(); // returns 1 queue.pop(); // returns 1 queue.empty(); // returns false 思路: 运用2个栈s_tmp,s ,因为栈是后进先出,队列是先进先出,所以,将每一次push的元素 x 都放到栈的栈底,先将栈 s 中的元素依次放入 s_tmp 中,再将 x 放入 s_tmp中,最后将 s_tmp中的元素再放入 s...