File tree 3 files changed +62
-0
lines changed
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,54 @@ func reverseWords(s string) string {
115
115
}
116
116
```
117
117
118
+ ### ** TypeScript**
119
+
120
+ API:
121
+
122
+ ``` ts
123
+ function reverseWords(s : string ): string {
124
+ return s .trim ().split (/ \s + / ).reverse ().join (' ' );
125
+ }
126
+ ```
127
+
128
+ 双指针:
129
+
130
+ ``` ts
131
+ function reverseWords(s : string ): string {
132
+ s = s .trim ();
133
+ const res = [];
134
+ let l = s .length - 1 ;
135
+ let r = s .length - 1 ;
136
+ while (l >= 0 ) {
137
+ while (s [l ] !== ' ' && l >= 0 ) {
138
+ l -- ;
139
+ }
140
+ res .push (s .substring (l + 1 , r + 1 ));
141
+ while (s [l ] === ' ' && l >= 0 ) {
142
+ l -- ;
143
+ }
144
+ r = l ;
145
+ }
146
+ return res .join (' ' );
147
+ }
148
+ ```
149
+
150
+ ### ** Rust**
151
+
152
+ ``` rust
153
+ impl Solution {
154
+ pub fn reverse_words (mut s : String ) -> String {
155
+ let mut res = s . trim (). split (' ' ). rev (). collect :: <Vec <& str >>();
156
+ for i in (0 .. res . len ()). rev () {
157
+ if res [i ] == "" {
158
+ res . remove (i );
159
+ }
160
+ }
161
+ res . join (" " )
162
+ }
163
+ }
164
+ ```
165
+
118
166
### ** ...**
119
167
120
168
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn reverse_words ( mut s : String ) -> String {
3
+ let mut res = s. trim ( ) . split ( ' ' ) . rev ( ) . collect :: < Vec < & str > > ( ) ;
4
+ for i in ( 0 ..res. len ( ) ) . rev ( ) {
5
+ if res[ i] == "" {
6
+ res. remove ( i) ;
7
+ }
8
+ }
9
+ res. join ( " " )
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ function reverseWords ( s : string ) : string {
2
+ return s . trim ( ) . split ( / \s + / ) . reverse ( ) . join ( ' ' ) ;
3
+ }
You can’t perform that action at this time.
0 commit comments