-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ225MyStack.js
64 lines (56 loc) · 1.4 KB
/
Q225MyStack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Description:
Implement a last-in-first-out (LIFO) stack using only two queues.
The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
var MyStack = function() {
this.stack = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
return this.stack.push(x);
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
const temp = [];
let len = this.stack.length;
while(--len) {
temp.push(this.stack.shift());
}
const ans = this.stack.shift();
this.stack = temp;
return ans;
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
const temp = [];
let len = this.stack.length;
while(--len) {
temp.push(this.stack.shift());
}
const ans = this.stack.shift();
temp.push(ans);
this.stack = temp;
return ans;
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.stack.length == 0;
};
//Runtime: 73 ms, faster than 65.27% of JavaScript online submissions for Implement Stack using Queues.
//Memory Usage: 41.9 MB, less than 42.46% of JavaScript online submissions for Implement Stack using Queues.