Skip to content

Commit ffc763a

Browse files
authored
Merge pull request gzc426#4 from qiuguomeng/qiuguomeng-patch-3
Create 。。。.md
2 parents 5f44baa + 8afa6c0 commit ffc763a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2018.11.28-leetcode151/。。。.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public String reverseWords(String s) {
2+
int i = s.length()-1,wordRight=-1;
3+
StringBuilder builder = new StringBuilder();
4+
while (i >= 0){
5+
if (s.charAt(i) == ' '){
6+
if (wordRight!=-1){
7+
if (builder.length()!=0){
8+
builder.append(' ');
9+
}
10+
builder.append(s.substring(i+1,wordRight+1));
11+
wordRight=-1;
12+
}
13+
i--;
14+
continue;
15+
}
16+
if (wordRight==-1)wordRight = i;
17+
i--;
18+
}
19+
//添加最后一个单词
20+
if (wordRight!=-1){
21+
if (builder.length()!=0){
22+
builder.append(' ');
23+
}
24+
builder.append(s.substring(i+1,wordRight+1));
25+
}
26+
return builder.toString();
27+
}

0 commit comments

Comments
 (0)