Skip to content

Commit c2cb413

Browse files
refactor 439
1 parent 51ce8af commit c2cb413

File tree

2 files changed

+65
-62
lines changed

2 files changed

+65
-62
lines changed

src/main/java/com/fishercoder/solutions/_439.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,38 @@
5151
*/
5252
public class _439 {
5353

54-
/**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,
55-
* example: https://discuss.leetcode.com/topic/64409/very-easy-1-pass-stack-solution-in-java-no-string-concat*/
56-
57-
public String parseTernary(String expression) {
58-
Deque<Character> stack = new ArrayDeque<>();
59-
Deque<Character> tmpStack = new ArrayDeque<>();
60-
for (char c : expression.toCharArray()) {
61-
stack.addFirst(c);
62-
}
63-
while (!stack.isEmpty()) {
64-
if (stack.peek() != '?') {
65-
tmpStack.addFirst(stack.pollFirst());
66-
} else {
67-
char char1 = tmpStack.removeFirst();
68-
tmpStack.removeFirst();//remove ':'
69-
char char2 = tmpStack.removeFirst();
70-
stack.removeFirst();//remove '?'
71-
char judge = stack.removeFirst();
72-
tmpStack.addFirst(judge == 'T' ? char1 : char2);
73-
while (!tmpStack.isEmpty()) {
74-
stack.addFirst(tmpStack.pollFirst());
75-
}
54+
public static class Solution1 {
55+
/**
56+
* Below is my original solution, but looking at Discuss, a more concise way is to use just one
57+
* 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
58+
*/
59+
60+
public String parseTernary(String expression) {
61+
Deque<Character> stack = new ArrayDeque<>();
62+
Deque<Character> tmpStack = new ArrayDeque<>();
63+
for (char c : expression.toCharArray()) {
64+
stack.addFirst(c);
7665
}
77-
if (stack.size() == 1) {
78-
break;
66+
while (!stack.isEmpty()) {
67+
if (stack.peek() != '?') {
68+
tmpStack.addFirst(stack.pollFirst());
69+
} else {
70+
char char1 = tmpStack.removeFirst();
71+
tmpStack.removeFirst();//remove ':'
72+
char char2 = tmpStack.removeFirst();
73+
stack.removeFirst();//remove '?'
74+
char judge = stack.removeFirst();
75+
tmpStack.addFirst(judge == 'T' ? char1 : char2);
76+
while (!tmpStack.isEmpty()) {
77+
stack.addFirst(tmpStack.pollFirst());
78+
}
79+
}
80+
if (stack.size() == 1) {
81+
break;
82+
}
7983
}
84+
return Character.toString(stack.removeFirst());
8085
}
81-
return Character.toString(stack.removeFirst());
8286
}
8387

8488
}

src/test/java/com/fishercoder/_439Test.java

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,40 @@
1010
* Created by fishercoder on 5/18/17.
1111
*/
1212
public class _439Test {
13-
private static _439 test;
14-
private static String expression;
15-
private static String expected;
16-
private static String actual;
17-
18-
@BeforeClass
19-
public static void setup() {
20-
test = new _439();
21-
}
22-
23-
@Test
24-
public void test1() {
25-
expression = "T?2:3";
26-
expected = "2";
27-
assertEquals(expected, test.parseTernary(expression));
28-
}
29-
30-
@Test
31-
public void test2() {
32-
expression = "F?1:T?4:5";
33-
expected = "4";
34-
assertEquals(expected, test.parseTernary(expression));
35-
}
36-
37-
@Test
38-
public void test3() {
39-
expression = "T?T?F:5:3";
40-
expected = "F";
41-
assertEquals(expected, test.parseTernary(expression));
42-
}
43-
44-
@Test
45-
public void test4() {
46-
expression = "T?T:F?T?1:2:F?3:4";
47-
expected = "T";
48-
assertEquals(expected, test.parseTernary(expression));
49-
}
13+
private static _439.Solution1 solution1;
14+
private static String expression;
15+
private static String expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _439.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
expression = "T?2:3";
25+
expected = "2";
26+
assertEquals(expected, solution1.parseTernary(expression));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
expression = "F?1:T?4:5";
32+
expected = "4";
33+
assertEquals(expected, solution1.parseTernary(expression));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
expression = "T?T?F:5:3";
39+
expected = "F";
40+
assertEquals(expected, solution1.parseTernary(expression));
41+
}
42+
43+
@Test
44+
public void test4() {
45+
expression = "T?T:F?T?1:2:F?3:4";
46+
expected = "T";
47+
assertEquals(expected, solution1.parseTernary(expression));
48+
}
5049
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy