Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9145fc

Browse files
committedOct 2, 2021
Add solution #232
1 parent abc9b1d commit b9145fc

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
5656
225|[Implement Stack using Queues](./0225-implement-stack-using-queues.js)|Easy|
5757
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
58+
232|[Implement Queue using Stacks](./0232-implement-queue-using-stacks.js)|Easy|
5859
234|[Palindrome Linked List](./0234-palindrome-linked-list.js)|Easy|
5960
237|[Delete Node in a Linked List](./0237-delete-node-in-a-linked-list.js)|Easy|
6061
242|[Valid Anagram](./0242-valid-anagram.js)|Easy|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* 232. Implement Queue using Stacks
3+
* https://leetcode.com/problems/implement-queue-using-stacks/
4+
* Difficulty: Easy
5+
*
6+
* Implement a first in first out (FIFO) queue using only two stacks.
7+
* The implemented queue should support all the functions of a normal
8+
* queue (push, peek, pop, and empty).
9+
*
10+
* Implement the MyQueue class:
11+
* - void push(int x) Pushes element x to the back of the queue.
12+
* - int pop() Removes the element from the front of the queue and returns it.
13+
* - int peek() Returns the element at the front of the queue.
14+
* - boolean empty() Returns true if the queue is empty, false otherwise.
15+
*/
16+
17+
18+
var MyQueue = function() {
19+
this.data = [];
20+
};
21+
22+
/**
23+
* @param {number} x
24+
* @return {void}
25+
*/
26+
MyQueue.prototype.push = function(x) {
27+
this.data.push(x);
28+
};
29+
30+
/**
31+
* @return {number}
32+
*/
33+
MyQueue.prototype.pop = function() {
34+
return this.data.shift();
35+
};
36+
37+
/**
38+
* @return {number}
39+
*/
40+
MyQueue.prototype.peek = function() {
41+
return this.data[0];
42+
};
43+
44+
/**
45+
* @return {boolean}
46+
*/
47+
MyQueue.prototype.empty = function() {
48+
return !this.data.length;
49+
};
50+
51+
/**
52+
* Your MyQueue object will be instantiated and called as such:
53+
* var obj = new MyQueue()
54+
* obj.push(x)
55+
* var param_2 = obj.pop()
56+
* var param_3 = obj.peek()
57+
* var param_4 = obj.empty()
58+
*/

0 commit comments

Comments
 (0)
Please sign in to comment.