-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path151. Reverse Words in a String.java
46 lines (43 loc) · 1.38 KB
/
151. Reverse Words in a String.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
class Solution {
public String reverseWords(String s) {
char[] sca=s.toCharArray();
StringBuilder sb=new StringBuilder();
//用于存储单词对应substring下标的stack
int[] startStack=new int[s.length()];
int[] endStack=new int[s.length()];
int top1=0;
int top2=0;
int cur=0;
while(cur<sca.length){
while(cur<sca.length&&sca[cur]==' '){//跳过前导空格
cur++;
}
int start=cur;
while(true){
if(cur<sca.length&&sca[cur]!=' '){
cur++;
}else{
if(cur>=sca.length){
break;
}else{
break;
}
}
}
startStack[top1++]=start;
endStack[top2++]=cur;
while(cur+1<sca.length&&sca[cur+1]==' '){//跳过单次间的多个空格
cur++;
}
cur++;
}
while(top1>0){//依次取栈顶的(start,end)对,append sb
if(top1!=1){
sb.append(s.substring(startStack[--top1],endStack[--top2])+' ');
}else{
sb.append(s.substring(startStack[--top1],endStack[--top2]));
}
}
return sb.toString();
}
}