Skip to content

Commit abc9b1d

Browse files
committed
Add solution #225
1 parent a83ea52 commit abc9b1d

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

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

0 commit comments

Comments
 (0)