forked from LeuisKen/LeetCodeTest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path115.js
More file actions
34 lines (30 loc) · 700 Bytes
/
115.js
File metadata and controls
34 lines (30 loc) · 700 Bytes
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
function MinStack() {
var stackData = [],
stackMin = []
this.push = function(newNum) {
if(stackMin.length === 0 || newNum < this.getMin()) {
stackMin.push(newNum)
} else {
var newMin = this.getMin()
stackMin.push(newMin)
}
stackData.push(newNum)
}
this.pop = function() {
if(stackData.length === 0) {
throw new Error('Your stack is empty.')
}
stackMin.pop()
return stackData.pop()
}
this.getMin = function() {
var len = stackMin.length
if(len === 0) {
throw new Error('Your stack is empty.')
}
return stackMin[len - 1]
}
this.top = function() {
return stackData[stackData.length - 1]
}
}