-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReverseWordsInAStringIII.java
More file actions
38 lines (36 loc) · 1.14 KB
/
ReverseWordsInAStringIII.java
File metadata and controls
38 lines (36 loc) · 1.14 KB
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
package array_and_matrix;
/**
* @Author: Wenhang Chen
* @Description:给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例 1:
* <p>
* 输入: "Let's take LeetCode contest"
* 输出: "s'teL ekat edoCteeL tsetnoc"
* @Date: Created in 8:59 12/10/2019
* @Modified by:
*/
public class ReverseWordsInAStringIII {
// 反转函数
private void reverse(char[] cm, int start, int end) {
for (int i = start; i <= end + start >> 1; i++) {
char tmp = cm[i];
cm[i] = cm[end + start - i];
cm[end + start - i] = tmp;
}
}
public String reverseWords(String s) {
if (s == null || s.length() < 1) return s;
int len = s.length();
int start = 0;
char[] cm = s.toCharArray();
for (int i = 0; i < len; i++) {
// 中途遇空格反转
if (cm[i] == ' ') {
reverse(cm, start, i - 1);
start = i + 1;
}
}
// 最后一个单词反转
reverse(cm, start, len - 1);
return String.valueOf(cm);
}
}