-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem151.java
59 lines (57 loc) · 1.42 KB
/
Problem151.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
56
57
58
59
class Problem151 {
public static void main(String[] args) {
String s = "easy it hello";
System.out.println("-" + new Problem151().reverseWords(s) + "-");
}
public String reverseWords(String s) {
int i = 0, j = s.length()-1;
char[] sarr = s.toCharArray();
while (i < j) {
char c = sarr[i];
sarr[i] = sarr[j];
sarr[j] = c;
i++;
j--;
}// reverse
// reverse word
// cba 331
int k = 0;
int start = 0;
while( k < sarr.length) {
if (sarr[k] == ' ' || k == sarr.length-1) {
// reverse
if (start < k) {
int end = sarr[k] == ' ' ? k-1: k;
while(start < end) {
char c = sarr[start];
sarr[start] = sarr[end];
sarr[end] = c;
start++;
end--;
}
start = ++k;
} else {
k++;
}
} else {
k++;
}
}
System.out.println("-" + new String(sarr) + "-");
int m = 0;
char pre = '\0';
for (int l = 0; l < sarr.length; l++) {
if (sarr[l] == ' ') {
if (pre != ' ' && l != sarr.length-1) {
sarr[m] = sarr[l];
m++;
}
} else {
sarr[m] = sarr[l];
m++;
}
pre = sarr[l];
}
return new String(sarr, 0, m).trim();
}
}