## LeetCode 232## Impletement Queue using StackclassMyQueue:def__init__(self):self.l1=[]## stack 1 = l1self.l2=[]## stack 2 = l2defpush(self,x:int)->None:## Push x onto queueself.l1.append(x)## The right of the list = the top of the stack = the back of the queuedef...
MyQueue queue =newMyQueue(); queue.push(1); queue.push(2); queue.peek();// 返回 1queue.pop();// 返回 1queue.empty();// 返回 false 说明: 你只能使用标准的栈操作 -- 也就是只有push to top,peek/pop from top,size, 和is empty操作是合法的。 你所使用的语言也许不支持栈。你可以使用...
myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false 提示: 1 <= x <= 9 最多调用 100 次 push、pop、peek 和 empty 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作) 来源:力扣(LeetCode) 链接:https://l...
isEmpty class MyQueue { private Stack<Integer> S = new Stack<Integer>(); private Stack<Integer> temp = new Stack<Integer>(); /** Initialize your data structure here. */ public MyQueue() { } /** Push element x to the back of queue. */ public void push(int x) { while(!S.is...
Implement Queue using Stacks 链接:https://leetcode.com/problems/implement-queue-using-stacks/ 思路 使用两个栈,stk1用作主要存储。假设已经往stk1里push一些元素,现在要pop,即取栈底的元素,则把上面的元素暂存到stk2里再取栈底。此时把stk2元素放回stk1可恢复原来的顺序,但是,如果下个操作还是pop,此时...
Push element x to the back of queue. :type x: int :rtype: void """ self.stack1.append(x) def dump_data(self): if not self.stack2: while self.stack1: self.stack2.append(self.stack1.pop()) def pop(self): """ Removes the element from in front of queue and returns that elem...
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. Example: MyQueue queue = new MyQueue();queue.push(1);queue.push(2);queue.peek(); // retur...
LeetCode 232. Implement Queue using Stacks class MyQueue { public: int stack[100005]; int stack2[100005]; int pos; int pos2; /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */...
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. ...
I use the same idea in my code, which is as follows. 1classQueue {2public:3//Push element x to the back of queue.4voidpush(intx) {5stack1.push(x);6}78//Removes the element from in front of queue.9voidpop(void) {10if(stack2.empty()) {11while(!stack1.empty()) {12intelem...