[LeetCode][JavaScript]Implement Queue using Stacks

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.

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • 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 called on an empty queue).

https://leetcode.com/problems/implement-queue-using-stacks/


2个栈实现队列。

栈1为主,栈2为辅。

push操作,先把栈2所有的元素都倒入栈1,然后对栈1压桟。

pop和peek操作,如果栈2为空,就把栈1所有元素都倒入栈2,然后对栈2出桟。

 1 /**
 2  * @constructor
 3  */
 4 var Queue = function() {
 5     this.stack1 = [];
 6     this.stack2 = [];
 7 };
 8
 9 /**
10  * @param {number} x
11  * @returns {void}
12  */
13 Queue.prototype.push = function(x) {
14     var len = this.stack2.length;
15     while(len--){
16         this.stack1.push(this.stack2.pop());
17     }
18     this.stack1.push(x);
19 };
20
21 /**
22  * @returns {void}
23  */
24 Queue.prototype.pop = function() {
25     if(this.stack2.length === 0){
26         var len = this.stack1.length;
27         while(len--){
28             this.stack2.push(this.stack1.pop());
29         }
30     }
31     return this.stack2.pop();
32 };
33
34 /**
35  * @returns {number}
36  */
37 Queue.prototype.peek = function() {
38     if(this.stack2.length === 0){
39         var len = this.stack1.length;
40         while(len--){
41             this.stack2.push(this.stack1.pop());
42         }
43     }
44     return this.stack2[this.stack2.length - 1];
45 };
46
47 /**
48  * @returns {boolean}
49  */
50 Queue.prototype.empty = function() {
51     if(this.stack1.length === 0 && this.stack2.length === 0){
52         return true;
53     }
54     return false;
55 };
时间: 2024-12-08 22:41:16

[LeetCode][JavaScript]Implement Queue using Stacks的相关文章

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译 用栈来实现队列的下列操作. push(x) -- 将元素x写入到队列的尾部 pop() -- 从队列首部移除元素 peek() -- 返回队列首部元素 empty() -- 返回队列是否为空 注意: 你必须使用一个只有标准操作的栈. 也就是说,只有push/pop/size/empty等操作是有效的. 栈可能不被原生支持,这取决于你所用的语言. 只要你只是用stack的标准操作,你可以用list或者deque(double-ended queue)来模拟栈. 你可以假设所有的操作都是有效的

[LeetCode] 232. 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. Notes: You

[LeetCode] 232 - 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. Notes: You

[LeetCode] 232. Implement Queue using Stacks Java

题目: 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. Notes:

[leetcode 234]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. Notes: You

(easy)LeetCode 232.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. Notes: You

leetCode(37):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. Notes: You

Java [Leetcode 232]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. Note