-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path557. Reverse Words in a String III.java
executable file
·51 lines (42 loc) · 1.35 KB
/
557. Reverse Words in a String III.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
E
tags: String
给一个String, 里面的Word被single space split开来, 目的是reverse里面所有的Word, 但preserve Word 和 space order.
#### Reverse function
- Reverse Words in a String II 的降级版, 去掉第一个overall reverse就好了
```
/*
Given a string, you need to reverse the order of characters
in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
*/
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() <= 1) {
return s;
}
char[] str = s.toCharArray();
int start = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == ' ') {
reverse(str, start, i - 1);
start = i + 1;
} else if (i == str.length - 1) {
reverse(str, start, i);
}
}//end for
return String.valueOf(str);
}
public void reverse(char[] s, int start, int end) {
while (start < end) {
char temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
}
```