Skip to content

Commit 8117da9

Browse files
committed
Java solution 20 && 42
1 parent 5b0c3f0 commit 8117da9

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

java/_020ValidParentheses.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
5+
* The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
6+
* <p>
7+
* Created by drfish on 6/7/2017.
8+
*/
9+
public class _020ValidParentheses {
10+
public boolean isValid(String s) {
11+
if (s == null) {
12+
return true;
13+
}
14+
Stack<Character> stack = new Stack<>();
15+
for (char c : s.toCharArray()) {
16+
switch (c) {
17+
case '(':
18+
stack.push(')');
19+
break;
20+
case '{':
21+
stack.push('}');
22+
break;
23+
case '[':
24+
stack.push(']');
25+
break;
26+
default:
27+
if (stack.isEmpty() || stack.pop() != c) {
28+
return false;
29+
}
30+
}
31+
}
32+
return stack.isEmpty();
33+
}
34+
35+
public static void main(String[] args) {
36+
_020ValidParentheses solution = new _020ValidParentheses();
37+
assert solution.isValid("()");
38+
assert solution.isValid("()[]()");
39+
assert !solution.isValid("(]");
40+
assert !solution.isValid("([)]");
41+
}
42+
}

java/_042TrappingRainWater.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
5+
* <p>
6+
* For example,
7+
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
8+
* <p>
9+
* The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
10+
* <p>
11+
* Created by drfish on 6/7/2017.
12+
*/
13+
public class _042TrappingRainWater {
14+
public int trap(int[] height) {
15+
if (height == null) {
16+
return 0;
17+
}
18+
int leftIndex = 0;
19+
int rightIndex = height.length - 1;
20+
int leftMax = 0;
21+
int rightMax = 0;
22+
int result = 0;
23+
24+
while (leftIndex <= rightIndex) {
25+
if (height[leftIndex] <= height[rightIndex]) {
26+
if (height[leftIndex] >= leftMax) {
27+
leftMax = height[leftIndex];
28+
} else {
29+
result += leftMax - height[leftIndex];
30+
}
31+
leftIndex++;
32+
} else {
33+
if (height[rightIndex] >= rightMax) {
34+
rightMax = height[rightIndex];
35+
} else {
36+
result += rightMax - height[rightIndex];
37+
}
38+
rightIndex--;
39+
}
40+
}
41+
return result;
42+
}
43+
44+
public static void main(String[] args) {
45+
_042TrappingRainWater solution = new _042TrappingRainWater();
46+
int[] height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
47+
assert 6 == solution.trap(height);
48+
assert 0 == solution.trap(new int[]{0, 2, 0});
49+
}
50+
}

0 commit comments

Comments
 (0)