-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode151.java
More file actions
41 lines (38 loc) · 1.27 KB
/
LeetCode151.java
File metadata and controls
41 lines (38 loc) · 1.27 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
39
40
41
public class LeetCode151 {
public static void main(String[] args) {
// 输入:s = "the sky is blue"
// 输出:"blue is sky the"
System.out.println(new Solution151().reverseWords("the sky is blue"));
// 输入:s = " hello world "
// 输出:"world hello"
System.out.println(new Solution151().reverseWords(" hello world "));
// 输入:s = "a good example"
// 输出:"example good a"
System.out.println(new Solution151().reverseWords("a good example"));
}
}
class Solution151 {
public String reverseWords(String s) {
s = new StringBuilder(s.strip()).reverse().toString();
// System.out.println(s);
StringBuilder result = new StringBuilder();
StringBuilder sb = new StringBuilder();
int p = 0;
while (p < s.length()) {
char c = s.charAt(p);
if (c == ' ') {
if (sb.length() > 0) {
result.append(sb.reverse());
result.append(' ');
}
sb = new StringBuilder();
} else {
sb.append(c);
}
p++;
}
sb.reverse();
result.append(sb.toString());
return result.toString();
}
}