Skip to content

Commit 02fc3f8

Browse files
refactor 186
1 parent d263652 commit 02fc3f8

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
/**
44
* 186. Reverse Words in a String II
5-
*
6-
* Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
5+
6+
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
77
88
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
99
@@ -14,29 +14,30 @@
1414
Could you do it in-place without allocating extra space?
1515
*/
1616
public class _186 {
17-
17+
public static class Solution1 {
1818
public void reverseWords(char[] s) {
19-
// Three steps to reverse
20-
// 1, reverse the whole sentence
21-
reverse(s, 0, s.length - 1);
22-
// 2, reverse each word
23-
int start = 0;
24-
for (int i = 0; i < s.length; i++) {
25-
if (s[i] == ' ') {
26-
reverse(s, start, i - 1);
27-
start = i + 1;
28-
}
19+
// Three steps to reverse
20+
// 1, reverse the whole sentence
21+
reverse(s, 0, s.length - 1);
22+
// 2, reverse each word
23+
int start = 0;
24+
for (int i = 0; i < s.length; i++) {
25+
if (s[i] == ' ') {
26+
reverse(s, start, i - 1);
27+
start = i + 1;
2928
}
30-
// 3, reverse the last word, if there is only one word this will solve the corner case
31-
reverse(s, start, s.length - 1);
29+
}
30+
// 3, reverse the last word, if there is only one word this will solve the corner case
31+
reverse(s, start, s.length - 1);
3232
}
3333

3434
private void reverse(char[] s, int start, int end) {
35-
while (start < end) {
36-
char temp = s[start];
37-
s[start++] = s[end];
38-
s[end--] = temp;
39-
}
35+
while (start < end) {
36+
char temp = s[start];
37+
s[start++] = s[end];
38+
s[end--] = temp;
39+
}
4040
}
41+
}
4142

4243
}

src/test/java/com/fishercoder/_186Test.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77
import static org.junit.Assert.assertArrayEquals;
88

99
public class _186Test {
10-
private static _186 test;
11-
private static char[] s;
12-
private static char[] expected;
10+
private static _186.Solution1 solution1;
11+
private static char[] s;
12+
private static char[] expected;
1313

14-
@BeforeClass
15-
public static void setup() {
16-
test = new _186();
17-
}
18-
19-
@Test
20-
public void test1() {
21-
s = new char[]{'h', 'i', '!'};
22-
test.reverseWords(s);
23-
expected = new char[]{'h', 'i', '!'};
24-
assertArrayEquals(expected, s);
25-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _186.Solution1();
17+
}
2618

19+
@Test
20+
public void test1() {
21+
s = new char[] {'h', 'i', '!'};
22+
solution1.reverseWords(s);
23+
expected = new char[] {'h', 'i', '!'};
24+
assertArrayEquals(expected, s);
25+
}
2726
}

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