forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_439.java
42 lines (37 loc) · 1.51 KB
/
_439.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
public class _439 {
public static class Solution1 {
/**
* Below is my original solution, but looking at Discuss, a more concise way is to use just one
* stack, process it from right to left, example: https://discuss.leetcode.com/topic/64409/very-easy-1-pass-stack-solution-in-java-no-string-concat
*/
public String parseTernary(String expression) {
Deque<Character> stack = new ArrayDeque<>();
Deque<Character> tmpStack = new ArrayDeque<>();
for (char c : expression.toCharArray()) {
stack.addFirst(c);
}
while (!stack.isEmpty()) {
if (stack.peek() != '?') {
tmpStack.addFirst(stack.pollFirst());
} else {
char char1 = tmpStack.removeFirst();
tmpStack.removeFirst();//remove ':'
char char2 = tmpStack.removeFirst();
stack.removeFirst();//remove '?'
char judge = stack.removeFirst();
tmpStack.addFirst(judge == 'T' ? char1 : char2);
while (!tmpStack.isEmpty()) {
stack.addFirst(tmpStack.pollFirst());
}
}
if (stack.size() == 1) {
break;
}
}
return Character.toString(stack.removeFirst());
}
}
}