Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.cdtft.leetcode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author : wangcheng | ||
* @date : 2020年05月11日 11:13 | ||
*/ | ||
public class MinStack { | ||
|
||
private Integer minValue; | ||
|
||
private List<Node> nodeList; | ||
|
||
public MinStack() { | ||
this.nodeList = new ArrayList<>(); | ||
} | ||
|
||
public void push(int x) { | ||
if (nodeList.size() == 0) { | ||
minValue = x; | ||
} | ||
|
||
nodeList.add(new Node(x - minValue, x)); | ||
if (x < minValue) { | ||
minValue = x; | ||
} | ||
} | ||
|
||
public void pop() { | ||
if (nodeList.size() == 0) { | ||
return; | ||
} | ||
Node node = nodeList.remove(nodeList.size() - 1); | ||
if (node.diff < 0) { | ||
refreshMinValue(node); | ||
} | ||
} | ||
|
||
public int top() { | ||
if (nodeList.size() == 0) { | ||
return Integer.MAX_VALUE; | ||
} | ||
Node node = nodeList.get(nodeList.size() - 1); | ||
return node.value; | ||
} | ||
|
||
public int getMin() { | ||
return minValue; | ||
} | ||
|
||
private void refreshMinValue(Node node) { | ||
minValue = node.value - node.diff; | ||
} | ||
|
||
private class Node { | ||
|
||
Integer diff; | ||
|
||
Integer value; | ||
|
||
public Node(Integer diff, Integer value) { | ||
this.diff = diff; | ||
this.value = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.cdtft.leetcode; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* ["MinStack","push","push","push","top","pop","getMin","pop","getMin","pop","push","top","getMin","push","top", | ||
* "getMin","pop","getMin"] | ||
* [[],[2147483646],[2147483646],[2147483647],[],[],[],[],[],[],[2147483647],[],[],[-2147483648],[],[],[],[]] | ||
*/ | ||
public class MinStackTest { | ||
|
||
@Test | ||
public void push() { | ||
MinStack minStack = new MinStack(); | ||
minStack.push(2147483646); | ||
minStack.push(2147483646); | ||
minStack.push(2147483647); | ||
System.out.println(minStack.top()); | ||
minStack.pop(); | ||
System.out.println(minStack.getMin()); | ||
minStack.pop(); | ||
System.out.println(minStack.getMin()); | ||
minStack.pop(); | ||
minStack.push(2147483647); | ||
System.out.println(minStack.top()); | ||
System.out.println(minStack.getMin()); | ||
minStack.push(-2147483648); | ||
System.out.println(minStack.top()); | ||
System.out.println(minStack.getMin()); | ||
minStack.pop(); | ||
System.out.println(minStack.getMin()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters