Skip to content

Commit 59fb466

Browse files
authored
Merge pull request gzc426#144 from fsd2018/master
Create 铁男神sama.md
2 parents 5af9c7e + 6338041 commit 59fb466

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

2018.11.28-leetcode151/铁男神sama.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
```java
33
public class Solution {
44
public String reverseWords(String s) {
@@ -19,3 +19,40 @@ public class Solution {
1919

2020
}
2121
```
22+
23+
24+
```java
25+
public class Solution {
26+
public String reverseWords(String s) {
27+
28+
char[] chas =s.toCharArray();
29+
reverse(chas,0,chas.length-1);
30+
int r=-1;
31+
int l=-1;
32+
for(int i=0;i<chas.length;i++){
33+
if(chas[i]!=' '){
34+
l=i==0||chas[i-1]==' '?i:l;
35+
r=i==chas.length-1||chas[i+1]==' '?i:r;
36+
}
37+
if(l!=-1&&r!=-1){
38+
reverse(chas,l,r);
39+
l=-1;
40+
r=-1;
41+
}
42+
}
43+
return String.valueOf(chas).trim().replaceAll("\\s+", " ");
44+
}
45+
public void reverse(char[] chas,int start,int end){
46+
char tmp=0;
47+
while(start<end){
48+
tmp=chas[start];
49+
chas[start]=chas[end];
50+
chas[end]=tmp;
51+
start++;
52+
end--;
53+
}
54+
}
55+
}
56+
```
57+
58+

0 commit comments

Comments
 (0)