Skip to content

Commit c44bf48

Browse files
refactor 151
1 parent e01aced commit c44bf48

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public String reverseWords(String s) {
2727
return stringBuilder.substring(0, stringBuilder.length() - 1).toString();
2828
}
2929
}
30+
3031
public static class Solution2 {
3132
public String reverseWords(String s) {
3233
int len = s.length();
@@ -50,11 +51,10 @@ public String reverseWords(String s) {
5051
}
5152
// word found
5253
String word = s.substring(i, j);
53-
if(result.length() == 0) {
54+
if (result.length() == 0) {
5455
result = word;
55-
}
56-
else {
57-
result = word + " "+ result;
56+
} else {
57+
result = word + " " + result;
5858
}
5959
i = j + 1;
6060
}

src/test/java/com/fishercoder/_151Test.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,43 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _151Test {
10-
private static _151.Solution1 solution1;
11-
private static _151.Solution2 solution2;
12-
private static String s;
13-
14-
@BeforeClass
15-
public static void setup()
16-
{
17-
solution1 = new _151.Solution1();
18-
solution2 = new _151.Solution2();
19-
}
20-
21-
@Test
22-
public void test1() {
23-
s = " ";
24-
assertEquals("", solution1.reverseWords(s));
25-
}
26-
27-
@Test
28-
public void test2() {
29-
s = " 1";
30-
assertEquals("1", solution1.reverseWords(s));
31-
}
32-
33-
@Test
34-
public void test3() {
35-
s = " a b ";
36-
assertEquals("b a", solution1.reverseWords(s));
37-
}
38-
39-
@Test
40-
public void test4() {
41-
s = "a b c";
42-
assertEquals("c b a", solution1.reverseWords(s));
43-
}
44-
45-
@Test
46-
public void test5() {
47-
s = " hello world ";
48-
assertEquals("world hello", solution2.reverseWords(s));
49-
}
10+
private static _151.Solution1 solution1;
11+
private static _151.Solution2 solution2;
12+
private static String s;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _151.Solution1();
17+
solution2 = new _151.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
s = " ";
23+
assertEquals("", solution1.reverseWords(s));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
s = " 1";
29+
assertEquals("1", solution1.reverseWords(s));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
s = " a b ";
35+
assertEquals("b a", solution1.reverseWords(s));
36+
}
37+
38+
@Test
39+
public void test4() {
40+
s = "a b c";
41+
assertEquals("c b a", solution1.reverseWords(s));
42+
}
43+
44+
@Test
45+
public void test5() {
46+
s = " hello world ";
47+
assertEquals("world hello", solution2.reverseWords(s));
48+
}
5049
}

0 commit comments

Comments
 (0)