Skip to content

Commit 4e3866f

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 03.02. 栈的最小值
1 parent 9bb0a91 commit 4e3866f

File tree

4 files changed

+252
-38
lines changed

4 files changed

+252
-38
lines changed

lcci/03.02.Min Stack/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,87 @@
66

77
## 解法
88
<!-- 这里可写通用的实现逻辑 -->
9+
利用辅助栈存放栈的最小元素。
910

1011

1112
### Python3
1213
<!-- 这里可写当前语言的特殊实现逻辑 -->
1314

1415
```python
16+
class MinStack:
1517

18+
def __init__(self):
19+
"""
20+
initialize your data structure here.
21+
"""
22+
self._s1 = []
23+
self._s2 = []
24+
25+
26+
def push(self, x: int) -> None:
27+
self._s1.append(x)
28+
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] >= x else self._s2[-1])
29+
30+
def pop(self) -> None:
31+
self._s1.pop()
32+
self._s2.pop()
33+
34+
def top(self) -> int:
35+
return self._s1[-1]
36+
37+
def getMin(self) -> int:
38+
return self._s2[-1]
39+
40+
41+
# Your MinStack object will be instantiated and called as such:
42+
# obj = MinStack()
43+
# obj.push(x)
44+
# obj.pop()
45+
# param_3 = obj.top()
46+
# param_4 = obj.getMin()
1647
```
1748

1849
### Java
1950
<!-- 这里可写当前语言的特殊实现逻辑 -->
2051

2152
```java
53+
class MinStack {
54+
private Stack<Integer> s1;
55+
private Stack<Integer> s2;
56+
57+
/** initialize your data structure here. */
58+
public MinStack() {
59+
s1 = new Stack<>();
60+
s2 = new Stack<>();
61+
}
62+
63+
public void push(int x) {
64+
s1.push(x);
65+
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
66+
}
67+
68+
public void pop() {
69+
s1.pop();
70+
s2.pop();
71+
}
72+
73+
public int top() {
74+
return s1.peek();
75+
}
76+
77+
public int getMin() {
78+
return s2.empty() ? -1 : s2.peek();
79+
}
80+
}
2281

82+
/**
83+
* Your MinStack object will be instantiated and called as such:
84+
* MinStack obj = new MinStack();
85+
* obj.push(x);
86+
* obj.pop();
87+
* int param_3 = obj.top();
88+
* int param_4 = obj.getMin();
89+
*/
2390
```
2491

2592
### ...

lcci/03.02.Min Stack/README_EN.md

+117-38
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,117 @@
1-
# [03.02. Min Stack](https://leetcode-cn.com/problems/min-stack-lcci)
2-
3-
## Description
4-
<p>How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.</p>
5-
6-
<p><strong>Example: </strong></p>
7-
8-
<pre>
9-
MinStack minStack = new MinStack();
10-
minStack.push(-2);
11-
minStack.push(0);
12-
minStack.push(-3);
13-
minStack.getMin(); --&gt; return -3.
14-
minStack.pop();
15-
minStack.top(); --&gt; return 0.
16-
minStack.getMin(); --&gt; return -2.</pre>
17-
18-
19-
20-
## Solutions
21-
22-
23-
### Python3
24-
25-
```python
26-
27-
```
28-
29-
### Java
30-
31-
```java
32-
33-
```
34-
35-
### ...
36-
```
37-
38-
```
1+
# [03.02. Min Stack](https://leetcode-cn.com/problems/min-stack-lcci)
2+
3+
## Description
4+
<p>How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time.</p>
5+
6+
7+
8+
<p><strong>Example: </strong></p>
9+
10+
11+
12+
<pre>
13+
14+
MinStack minStack = new MinStack();
15+
16+
minStack.push(-2);
17+
18+
minStack.push(0);
19+
20+
minStack.push(-3);
21+
22+
minStack.getMin(); --&gt; return -3.
23+
24+
minStack.pop();
25+
26+
minStack.top(); --&gt; return 0.
27+
28+
minStack.getMin(); --&gt; return -2.</pre>
29+
30+
31+
32+
33+
## Solutions
34+
35+
36+
### Python3
37+
38+
```python
39+
class MinStack:
40+
41+
def __init__(self):
42+
"""
43+
initialize your data structure here.
44+
"""
45+
self._s1 = []
46+
self._s2 = []
47+
48+
49+
def push(self, x: int) -> None:
50+
self._s1.append(x)
51+
self._s2.append(x if len(self._s2) == 0 or self._s2[-1] >= x else self._s2[-1])
52+
53+
def pop(self) -> None:
54+
self._s1.pop()
55+
self._s2.pop()
56+
57+
def top(self) -> int:
58+
return self._s1[-1]
59+
60+
def getMin(self) -> int:
61+
return self._s2[-1]
62+
63+
64+
# Your MinStack object will be instantiated and called as such:
65+
# obj = MinStack()
66+
# obj.push(x)
67+
# obj.pop()
68+
# param_3 = obj.top()
69+
# param_4 = obj.getMin()
70+
```
71+
72+
### Java
73+
74+
```java
75+
class MinStack {
76+
private Stack<Integer> s1;
77+
private Stack<Integer> s2;
78+
79+
/** initialize your data structure here. */
80+
public MinStack() {
81+
s1 = new Stack<>();
82+
s2 = new Stack<>();
83+
}
84+
85+
public void push(int x) {
86+
s1.push(x);
87+
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
88+
}
89+
90+
public void pop() {
91+
s1.pop();
92+
s2.pop();
93+
}
94+
95+
public int top() {
96+
return s1.peek();
97+
}
98+
99+
public int getMin() {
100+
return s2.empty() ? -1 : s2.peek();
101+
}
102+
}
103+
104+
/**
105+
* Your MinStack object will be instantiated and called as such:
106+
* MinStack obj = new MinStack();
107+
* obj.push(x);
108+
* obj.pop();
109+
* int param_3 = obj.top();
110+
* int param_4 = obj.getMin();
111+
*/
112+
```
113+
114+
### ...
115+
```
116+
117+
```

lcci/03.02.Min Stack/Solution.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class MinStack {
2+
private Stack<Integer> s1;
3+
private Stack<Integer> s2;
4+
5+
/** initialize your data structure here. */
6+
public MinStack() {
7+
s1 = new Stack<>();
8+
s2 = new Stack<>();
9+
}
10+
11+
public void push(int x) {
12+
s1.push(x);
13+
s2.push(s2.empty() || s2.peek() >= x ? x : s2.peek());
14+
}
15+
16+
public void pop() {
17+
s1.pop();
18+
s2.pop();
19+
}
20+
21+
public int top() {
22+
return s1.peek();
23+
}
24+
25+
public int getMin() {
26+
return s2.empty() ? -1 : s2.peek();
27+
}
28+
}
29+
30+
/**
31+
* Your MinStack object will be instantiated and called as such:
32+
* MinStack obj = new MinStack();
33+
* obj.push(x);
34+
* obj.pop();
35+
* int param_3 = obj.top();
36+
* int param_4 = obj.getMin();
37+
*/

lcci/03.02.Min Stack/Solution.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class MinStack:
2+
3+
def __init__(self):
4+
"""
5+
initialize your data structure here.
6+
"""
7+
self._s1 = []
8+
self._s2 = []
9+
10+
def push(self, x: int) -> None:
11+
self._s1.append(x)
12+
self._s2.append(x if len(self._s2) ==
13+
0 or self._s2[-1] >= x else self._s2[-1])
14+
15+
def pop(self) -> None:
16+
self._s1.pop()
17+
self._s2.pop()
18+
19+
def top(self) -> int:
20+
return self._s1[-1]
21+
22+
def getMin(self) -> int:
23+
return self._s2[-1]
24+
25+
26+
# Your MinStack object will be instantiated and called as such:
27+
# obj = MinStack()
28+
# obj.push(x)
29+
# obj.pop()
30+
# param_3 = obj.top()
31+
# param_4 = obj.getMin()

0 commit comments

Comments
 (0)