File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change 1
-
1
+
2
2
``` java
3
3
public class Solution {
4
4
public String reverseWords (String s ) {
@@ -19,3 +19,40 @@ public class Solution {
19
19
20
20
}
21
21
```
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
+
You can’t perform that action at this time.
0 commit comments