Skip to content

Commit 714d364

Browse files
refactor 155
1 parent 90d0a21 commit 714d364

File tree

1 file changed

+35
-35
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+35
-35
lines changed
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.Stack;
54

65
/**
@@ -25,47 +24,48 @@
2524

2625
public class _155 {
2726

28-
public static class MinStack {
29-
private Stack<Integer> stack;
30-
private int min;
31-
32-
/**
33-
* initialize your data structure here.
34-
*/
35-
public MinStack() {
36-
stack = new Stack();
37-
min = Integer.MAX_VALUE;
38-
}
27+
public static class Solution1 {
28+
class MinStack {
29+
private Stack<Integer> stack;
30+
private int min;
3931

40-
public void push(int x) {
41-
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
42-
* this way, when popping, we could always get the next minimum one in constant time.*/
43-
if (x <= min) {
44-
stack.push(min);
45-
min = x;
32+
/**
33+
* initialize your data structure here.
34+
*/
35+
public MinStack() {
36+
stack = new Stack();
37+
min = Integer.MAX_VALUE;
4638
}
47-
stack.push(x);
48-
}
4939

50-
public void pop() {
51-
if (min == stack.peek()) {
52-
stack.pop();
53-
min = stack.pop();
54-
} else {
55-
stack.pop();
40+
public void push(int x) {
41+
/**All the trick happens here, we push the second minimum number onto the stack before we push the newer one,
42+
* this way, when popping, we could always get the next minimum one in constant time.*/
43+
if (x <= min) {
44+
stack.push(min);
45+
min = x;
46+
}
47+
stack.push(x);
5648
}
57-
if (stack.isEmpty()) {
58-
min = Integer.MAX_VALUE;
49+
50+
public void pop() {
51+
if (min == stack.peek()) {
52+
stack.pop();
53+
min = stack.pop();
54+
} else {
55+
stack.pop();
56+
}
57+
if (stack.isEmpty()) {
58+
min = Integer.MAX_VALUE;
59+
}
5960
}
60-
}
6161

62-
public int top() {
63-
return stack.peek();
64-
}
62+
public int top() {
63+
return stack.peek();
64+
}
6565

66-
public int getMin() {
67-
return min;
66+
public int getMin() {
67+
return min;
68+
}
6869
}
6970
}
70-
7171
}

0 commit comments

Comments
 (0)