-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path151.反转字符串中的单词.java
55 lines (45 loc) · 1.36 KB
/
151.反转字符串中的单词.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
52
53
54
55
/*
* @lc app=leetcode.cn id=151 lang=java
*
* [151] 反转字符串中的单词
*/
// @lc code=start
class Solution {
// this problem is interesting, refer to leetcode official for
// more solutions
public String reverseWords(String s) {
int sLen = s.length();
// to store an temporary extra space
char[] result = new char[sLen + 1];
// find first non-space char in s, then for s[first:],
// every word is followed by 1 or more space
int first = 0;
while (s.charAt(first) == ' ') {
first++;
}
int i = sLen - 1;
int j = 0;
while (i >= first) {
// for a word followed by 1 or more space,
// find start and end of the word
int wordEnd = i;
while (wordEnd >=0 && s.charAt(wordEnd) == ' ') {
wordEnd--;
}
int wordStart = wordEnd;
while (wordStart >=0 && s.charAt(wordStart) != ' ') {
wordStart--;
}
// copy a word from s to result
for (int k = wordStart + 1; k <= wordEnd; k++) {
result[j++] = s.charAt(k);
}
result[j++] = ' ';
i = wordStart;
}
// from result[] build a string
j--;
return new String(result, 0, j);
}
}
// @lc code=end