Skip to content

Commit fec4dd7

Browse files
committed
Add solution 155 [Java]
1 parent f1e9a61 commit fec4dd7

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3333
| 083 | [Remove Duplicates from Sorted List](https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
3434
| 136 | [Single Number](https://github.com/doocs/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
3535
| 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` |
3637
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
3738
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
3839
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |

solution/155.Min Stack/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
```

solution/155.Min Stack/Solution.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
*/

0 commit comments

Comments
 (0)