|
| 1 | +# 155. 最小栈 |
| 2 | + |
| 3 | +[url](https://leetcode-cn.com/problems/min-stack/) |
| 4 | + |
| 5 | + |
| 6 | +## 题目 |
| 7 | + |
| 8 | +设计一个支持 `push` `,pop` `,top` 操作,并能在常数时间内检索到最小元素的栈。 |
| 9 | + |
| 10 | +`push(x)` —— 将元素 x 推入栈中。 |
| 11 | +`pop()` —— 删除栈顶的元素。 |
| 12 | +`top()` —— 获取栈顶元素。 |
| 13 | +`getMin()` —— 检索栈中的最小元素。 |
| 14 | + |
| 15 | + |
| 16 | +``` |
| 17 | +输入: |
| 18 | +["MinStack","push","push","push","getMin","pop","top","getMin"] |
| 19 | +[[],[-2],[0],[-3],[],[],[],[]] |
| 20 | +输出: |
| 21 | +[null,null,null,null,-3,null,0,-2] |
| 22 | +解释: |
| 23 | +MinStack minStack = new MinStack(); |
| 24 | +minStack.push(-2); |
| 25 | +minStack.push(0); |
| 26 | +minStack.push(-3); |
| 27 | +minStack.getMin(); --> 返回 -3. |
| 28 | +minStack.pop(); |
| 29 | +minStack.top(); --> 返回 0. |
| 30 | +minStack.getMin(); --> 返回 -2. |
| 31 | +``` |
| 32 | + |
| 33 | +## 方法 |
| 34 | + |
| 35 | + |
| 36 | +双栈即可 |
| 37 | +- 数据栈与最小栈 |
| 38 | + |
| 39 | +## code |
| 40 | + |
| 41 | +### js |
| 42 | + |
| 43 | +```js |
| 44 | +let MinStack = function() { |
| 45 | + this.dataStack = []; |
| 46 | + this.minStack = []; |
| 47 | + this.min = Infinity; |
| 48 | +} |
| 49 | +MinStack.prototype.push = function(x) { |
| 50 | + this.dataStack.push(x); |
| 51 | + this.min = Math.min(this.min, x); |
| 52 | + this.minStack.push(this.min); |
| 53 | +} |
| 54 | +MinStack.prototype.pop = function() { |
| 55 | + this.dataStack.pop(); |
| 56 | + this.minStack.pop(); |
| 57 | + this.min = this.minStack.length === 0 ? Infinity : this.minStack[this.minStack.length - 1]; |
| 58 | +} |
| 59 | +MinStack.prototype.top = function() { |
| 60 | + return this.dataStack[this.dataStack.length - 1]; |
| 61 | +} |
| 62 | +MinStack.prototype.getMin = function() { |
| 63 | + return this.min; |
| 64 | +} |
| 65 | +var test = new MinStack(); |
| 66 | +test.push(2); |
| 67 | +test.push(1); |
| 68 | +test.push(3); |
| 69 | +// test.pop(); |
| 70 | +console.log(test.getMin()); |
| 71 | +console.log(test.top()); |
| 72 | +``` |
| 73 | + |
| 74 | +### go |
| 75 | + |
| 76 | +```go |
| 77 | +const ( |
| 78 | + INT_MAX = int(^uint((0)) >> 1) |
| 79 | + INT_MIN = ^INT_MAX |
| 80 | +) |
| 81 | +var min = INT_MAX |
| 82 | +var s1 = []int{} |
| 83 | +var s2 = []int{} |
| 84 | +func push(x int) { |
| 85 | + s1 = append(s1, x) |
| 86 | + min = Min(min, x) |
| 87 | + s2 = append(s2, min) |
| 88 | +} |
| 89 | +func pop() { |
| 90 | + s1 = s1[:len(s1) - 1] |
| 91 | + s2 = s2[:len(s2) - 1] |
| 92 | + if len(s2) == 0 { |
| 93 | + min = INT_MAX |
| 94 | + } else { |
| 95 | + min = s2[len(s2) - 1] |
| 96 | + } |
| 97 | +} |
| 98 | +func top() int { |
| 99 | + return s1[len(s1) - 1] |
| 100 | +} |
| 101 | +func getMin() int { |
| 102 | + return min |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### java |
| 107 | + |
| 108 | +```java |
| 109 | +class MinStack { |
| 110 | + private Stack<Integer> dataStack; |
| 111 | + private Stack<Integer> minStack; |
| 112 | + private int min; |
| 113 | + /** initialize your data structure here. */ |
| 114 | + public MinStack() { |
| 115 | + dataStack = new Stack<>(); |
| 116 | + minStack = new Stack<>(); |
| 117 | + min = Integer.MAX_VALUE; |
| 118 | + } |
| 119 | + public void push(int x) { |
| 120 | + dataStack.push(x); |
| 121 | + min = Math.min(min, x); |
| 122 | + minStack.push(min); |
| 123 | + } |
| 124 | + public void pop() { |
| 125 | + dataStack.pop(); |
| 126 | + minStack.pop(); |
| 127 | + min = minStack.isEmpty() ? Integer.MAX_VALUE : minStack.peek(); |
| 128 | + } |
| 129 | + public int top() { |
| 130 | + return dataStack.peek(); |
| 131 | + } |
| 132 | + public int getMin() { |
| 133 | + return min; |
| 134 | + } |
| 135 | +} |
| 136 | +/** |
| 137 | + * Your MinStack object will be instantiated and called as such: |
| 138 | + * MinStack obj = new MinStack(); |
| 139 | + * obj.push(x); |
| 140 | + * obj.pop(); |
| 141 | + * int param_3 = obj.top(); |
| 142 | + * int param_4 = obj.getMin(); |
| 143 | + */ |
| 144 | +``` |
| 145 | + |
0 commit comments