Skip to content

Commit 733f6d7

Browse files
authored
Create TheRocket.md
1 parent 1bac179 commit 733f6d7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2018.12.2-leetcode557/TheRocket.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
```java
2+
class Solution {
3+
public String reverseWords(String s) {
4+
char[] cs = s.toCharArray();
5+
int i = 0;
6+
while (i < cs.length) {
7+
int begin = i;
8+
while (i < cs.length && cs[i] != ' ') {
9+
++i;
10+
}
11+
reverse(cs, begin, i - 1);
12+
++i;
13+
}
14+
return new String(cs);
15+
}
16+
17+
private void swap(char[] cs, int i, int j) {
18+
char tmp = cs[i];
19+
cs[i] = cs[j];
20+
cs[j] = tmp;
21+
}
22+
23+
private void reverse(char[] cs, int i, int j) {
24+
while (i < j) {
25+
swap(cs, i++, j--);
26+
}
27+
}
28+
}
29+
```

0 commit comments

Comments
 (0)