We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 7eddd47 + 9829e6c commit d8c124bCopy full SHA for d8c124b
LeetCode/155. Min Stack/Solution.java
@@ -0,0 +1,27 @@
1
+class MinStack {
2
+ int min = Integer.MAX_VALUE;
3
+ Stack<Integer> stack = new Stack<Integer>();
4
+ public void push(int x) {
5
+ // only push the old minimum value when the current
6
+ // minimum value changes after pushing the new value x
7
+ if(x <= min){
8
+ stack.push(min);
9
+ min=x;
10
+ }
11
+ stack.push(x);
12
13
+
14
+ public void pop() {
15
+ // if pop operation could result in the changing of the current minimum value,
16
+ // pop twice and change the current minimum value to the last minimum value.
17
+ if(stack.pop() == min) min=stack.pop();
18
19
20
+ public int top() {
21
+ return stack.peek();
22
23
24
+ public int getMin() {
25
+ return min;
26
27
+}
0 commit comments