Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ddc166e

Browse files
refactor 186
1 parent 9ea9c21 commit ddc166e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 186. Reverse Words in a String II
5+
*
46
* Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
57
68
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
@@ -32,10 +34,8 @@ public void reverseWords(char[] s) {
3234
private void reverse(char[] s, int start, int end) {
3335
while (start < end) {
3436
char temp = s[start];
35-
s[start] = s[end];
36-
s[end] = temp;
37-
start++;
38-
end--;
37+
s[start++] = s[end];
38+
s[end--] = temp;
3939
}
4040
}
4141

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._186;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _186Test {
10+
private static _186 test;
11+
private static char[] s;
12+
private static char[] expected;
13+
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+
}
26+
27+
}

0 commit comments

Comments
 (0)