Skip to content

Commit 46bee31

Browse files
committed
Java solution 155 && 173
1 parent d0fdb9b commit 46bee31

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

java/_155MinStack.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
5+
* <p>
6+
* push(x) -- Push element x onto stack.
7+
* pop() -- Removes the element on top of the stack.
8+
* top() -- Get the top element.
9+
* getMin() -- Retrieve the minimum element in the stack.
10+
* <p>
11+
* Example:
12+
* MinStack minStack = new MinStack();
13+
* minStack.push(-2);
14+
* minStack.push(0);
15+
* minStack.push(-3);
16+
* minStack.getMin(); --> Returns -3.
17+
* minStack.pop();
18+
* minStack.top(); --> Returns 0.
19+
* minStack.getMin(); --> Returns -2.
20+
* <p>
21+
* Created by drfish on 6/8/2017.
22+
*/
23+
public class _155MinStack {
24+
public class MinStack {
25+
Stack<Integer> stack;
26+
Stack<Integer> minStack;
27+
28+
/**
29+
* initialize your data structure here.
30+
*/
31+
public MinStack() {
32+
stack = new Stack<>();
33+
minStack = new Stack<>();
34+
}
35+
36+
public void push(int x) {
37+
stack.push(x);
38+
if (minStack.isEmpty() || minStack.peek() >= x) {
39+
minStack.push(x);
40+
}
41+
}
42+
43+
public void pop() {
44+
int x = stack.pop();
45+
if (x == minStack.peek()) {
46+
minStack.pop();
47+
}
48+
}
49+
50+
public int top() {
51+
return stack.peek();
52+
}
53+
54+
public int getMin() {
55+
return minStack.peek();
56+
}
57+
}
58+
59+
public static void main(String[] args) {
60+
MinStack minStack = new _155MinStack().new MinStack();
61+
minStack.push(-2);
62+
minStack.push(0);
63+
minStack.push(-3);
64+
assert -3 == minStack.getMin();
65+
minStack.pop();
66+
assert 0 == minStack.top();
67+
assert -2 == minStack.getMin();
68+
}
69+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
5+
* Calling next() will return the next smallest number in the BST.
6+
* <p>
7+
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
8+
* <p>
9+
* Credits:
10+
* Special thanks to @ts for adding this problem and creating all test cases.
11+
* <p>
12+
* Created by drfish on 6/8/2017.
13+
*/
14+
public class _173BinarySearchTreeIterator {
15+
public class BSTIterator {
16+
private Stack<TreeNode> stack;
17+
private TreeNode node;
18+
19+
public BSTIterator(TreeNode root) {
20+
stack = new Stack<>();
21+
node = root;
22+
}
23+
24+
/**
25+
* @return whether we have a next smallest number
26+
*/
27+
public boolean hasNext() {
28+
return node != null || !stack.isEmpty();
29+
}
30+
31+
/**
32+
* @return the next smallest number
33+
*/
34+
public int next() {
35+
while (node != null) {
36+
stack.push(node);
37+
node = node.left;
38+
}
39+
TreeNode temp = stack.pop();
40+
node = temp.right;
41+
return temp.val;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)