-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathMinStack.java
More file actions
43 lines (34 loc) · 707 Bytes
/
MinStack.java
File metadata and controls
43 lines (34 loc) · 707 Bytes
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
package leetcode;
import java.util.Stack;
/**
* Created by nikoo28 on 7/19/19 2:36 AM
*/
class MinStack {
private Stack<Integer> mStack = new Stack<>();
private Stack<Integer> mMinStack = new Stack<>();
public void push(int x) {
mStack.push(x);
if (mMinStack.size() != 0) {
int min = mMinStack.peek();
if (x <= min) {
mMinStack.push(x);
}
} else {
mMinStack.push(x);
}
}
public void pop() {
int x = mStack.pop();
if (mMinStack.size() != 0) {
if (x == mMinStack.peek()) {
mMinStack.pop();
}
}
}
public int top() {
return mStack.peek();
}
public int getMin() {
return mMinStack.peek();
}
}