-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155.最小栈.js
More file actions
120 lines (100 loc) · 2.38 KB
/
155.最小栈.js
File metadata and controls
120 lines (100 loc) · 2.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* @lc app=leetcode.cn id=155 lang=javascript
*
* [155] 最小栈
*/
// @lc code=start
var MinStack = function() {
this.stack = []
};
MinStack.prototype.parentIndex = function(index) {
return Math.floor((index - 1) / 2)
};
MinStack.prototype.leftChildIndex = function(index) {
return 2 * index + 1
};
MinStack.prototype.rightChildIndex = function(index) {
return 2 * index + 2
};
MinStack.prototype.shiftUp = function (index) {
while (index >= 0 && this.stack[this.parentIndex(index)] > this.stack[index]) {
let temp = this.stack[this.parentIndex(index)]
this.stack[this.parentIndex(index)] = this.stack[index]
this.stack[index] = temp
index = this.parentIndex(index)
}
}
MinStack.prototype.shiftDown = function (index) {
while (this.leftChildIndex(index) < this.stack.length) {
let i = this.leftChildIndex(index)
// 找出左右两个孩子中最小的那个孩子的索引
if (i + 1 < this.stack.length && this.stack[i + 1] < this.stack[i]) {
i = this.rightChildIndex(index)
}
if (this.stack[index] < this.stack[i]) {
break
}
const temp = this.stack[index]
this.stack[index] = this.stack[i]
this.stack[i] = temp
index = i
}
}
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this.stack.push(val)
this.shiftUp(this.stack.length - 1)
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if(this.stack.length === 0) {
return
}
const temp = this.stack[0]
this.stack[0] = this.stack[this.stack.length - 1]
this.stack[this.stack.length - 1] = temp
this.stack.pop()
this.shiftDown(0)
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack[this.stack.length - 1]
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.stack[0]
};
// const obj = new MinStack()
// obj.push(-2)
// obj.push(0)
// obj.push(-3)
// console.log(obj.getMin())
// console.log(obj.pop())
// console.log(obj.top())
// console.log(obj.getMin())
const obj = new MinStack()
obj.push(-2)
obj.push(0)
obj.push(-1)
console.log(obj.getMin())
console.log(obj.top())
console.log(obj.pop())
console.log(obj.getMin())
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
// @lc code=end