File tree Expand file tree Collapse file tree 2 files changed +31
-4
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +31
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
+ * 186. Reverse Words in a String II
5
+ *
4
6
* Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
5
7
6
8
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) {
32
34
private void reverse (char [] s , int start , int end ) {
33
35
while (start < end ) {
34
36
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 ;
39
39
}
40
40
}
41
41
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments