File tree Expand file tree Collapse file tree 3 files changed +104
-0
lines changed Expand file tree Collapse file tree 3 files changed +104
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
33
33
| 083 | [ Remove Duplicates from Sorted List] ( https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List ) | ` Linked List ` |
34
34
| 136 | [ Single Number] ( https://github.com/doocs/leetcode/tree/master/solution/136.Single%20Number ) | ` Hash Table ` , ` Bit Manipulation ` |
35
35
| 141 | [ Linked List Cycle] ( https://github.com/doocs/leetcode/tree/master/solution/141.Linked%20List%20Cycle ) | ` Linked List ` , ` Two Pointers ` |
36
+ | 155 | [ Min Stack] ( https://github.com/doocs/leetcode/tree/master/solution/155.Min%20Stack ) | ` Stack ` , ` Design ` |
36
37
| 189 | [ Rotate Array] ( https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array ) | ` Array ` |
37
38
| 198 | [ House Robber] ( https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber ) | ` Dynamic Programming ` |
38
39
| 203 | [ Remove Linked List Elements] ( https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements ) | ` Linked List ` |
Original file line number Diff line number Diff line change
1
+ ## 最小栈
2
+ ### 题目描述
3
+
4
+ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
5
+
6
+ - push(x) -- 将元素 x 推入栈中。
7
+ - pop() -- 删除栈顶的元素。
8
+ - top() -- 获取栈顶元素。
9
+ - getMin() -- 检索栈中的最小元素。
10
+
11
+ ** 示例:**
12
+ ```
13
+ MinStack minStack = new MinStack();
14
+ minStack.push(-2);
15
+ minStack.push(0);
16
+ minStack.push(-3);
17
+ minStack.getMin(); --> 返回 -3.
18
+ minStack.pop();
19
+ minStack.top(); --> 返回 0.
20
+ minStack.getMin(); --> 返回 -2.
21
+ ```
22
+
23
+ ### 解法
24
+ 创建两个栈 ` stack ` , ` help ` ,` help ` 作为辅助栈,每次压栈时,若 ` help ` 栈顶元素大于/等于要压入的元素 ` x ` ,或者 ` help ` 栈为空,则压入 ` x ` ,否则重复压入栈顶元素。取最小元素时,从 ` help ` 中获取栈顶元素即可。
25
+
26
+ ``` java
27
+ class MinStack {
28
+
29
+ private Stack<Integer > stack;
30
+ private Stack<Integer > help;
31
+
32
+ /* * initialize your data structure here. */
33
+ public MinStack () {
34
+ stack = new Stack<> ();
35
+ help = new Stack<> ();
36
+ }
37
+
38
+ public void push (int x ) {
39
+ stack. push(x);
40
+ help. push(help. isEmpty() || help. peek() >= x ? x : help. peek());
41
+ }
42
+
43
+ public void pop () {
44
+ stack. pop();
45
+ help. pop();
46
+ }
47
+
48
+ public int top () {
49
+ return stack. peek();
50
+ }
51
+
52
+ public int getMin () {
53
+ return help. peek();
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Your MinStack object will be instantiated and called as such:
59
+ * MinStack obj = new MinStack();
60
+ * obj.push(x);
61
+ * obj.pop();
62
+ * int param_3 = obj.top();
63
+ * int param_4 = obj.getMin();
64
+ */
65
+ ```
Original file line number Diff line number Diff line change
1
+ class MinStack {
2
+
3
+ private Stack <Integer > stack ;
4
+ private Stack <Integer > help ;
5
+
6
+ /** initialize your data structure here. */
7
+ public MinStack () {
8
+ stack = new Stack <>();
9
+ help = new Stack <>();
10
+ }
11
+
12
+ public void push (int x ) {
13
+ stack .push (x );
14
+ help .push (help .isEmpty () || help .peek () >= x ? x : help .peek ());
15
+ }
16
+
17
+ public void pop () {
18
+ stack .pop ();
19
+ help .pop ();
20
+ }
21
+
22
+ public int top () {
23
+ return stack .peek ();
24
+ }
25
+
26
+ public int getMin () {
27
+ return help .peek ();
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Your MinStack object will be instantiated and called as such:
33
+ * MinStack obj = new MinStack();
34
+ * obj.push(x);
35
+ * obj.pop();
36
+ * int param_3 = obj.top();
37
+ * int param_4 = obj.getMin();
38
+ */
You can’t perform that action at this time.
0 commit comments