File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
solution/0900-0999/0917.Reverse Only Letters Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,31 @@ func reverseOnlyLetters(s string) string {
178
178
}
179
179
```
180
180
181
+ ### ** Rust**
182
+
183
+ ``` rust
184
+ impl Solution {
185
+ pub fn reverse_only_letters (s : String ) -> String {
186
+ let mut cs : Vec <char > = s . chars (). collect ();
187
+ let n = cs . len ();
188
+ let mut l = 0 ;
189
+ let mut r = n - 1 ;
190
+ while l < r {
191
+ if ! cs [l ]. is_ascii_alphabetic () {
192
+ l += 1 ;
193
+ } else if ! cs [r ]. is_ascii_alphabetic () {
194
+ r -= 1 ;
195
+ } else {
196
+ cs . swap (l , r );
197
+ l += 1 ;
198
+ r -= 1 ;
199
+ }
200
+ }
201
+ cs . iter (). collect ()
202
+ }
203
+ }
204
+ ```
205
+
181
206
### ** ...**
182
207
183
208
```
Original file line number Diff line number Diff line change @@ -146,6 +146,31 @@ func reverseOnlyLetters(s string) string {
146
146
}
147
147
```
148
148
149
+ ### ** Rust**
150
+
151
+ ``` rust
152
+ impl Solution {
153
+ pub fn reverse_only_letters (s : String ) -> String {
154
+ let mut cs : Vec <char > = s . chars (). collect ();
155
+ let n = cs . len ();
156
+ let mut l = 0 ;
157
+ let mut r = n - 1 ;
158
+ while l < r {
159
+ if ! cs [l ]. is_ascii_alphabetic () {
160
+ l += 1 ;
161
+ } else if ! cs [r ]. is_ascii_alphabetic () {
162
+ r -= 1 ;
163
+ } else {
164
+ cs . swap (l , r );
165
+ l += 1 ;
166
+ r -= 1 ;
167
+ }
168
+ }
169
+ cs . iter (). collect ()
170
+ }
171
+ }
172
+ ```
173
+
149
174
### ** ...**
150
175
151
176
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn reverse_only_letters ( s : String ) -> String {
3
+ let mut cs: Vec < char > = s. chars ( ) . collect ( ) ;
4
+ let n = cs. len ( ) ;
5
+ let mut l = 0 ;
6
+ let mut r = n - 1 ;
7
+ while l < r {
8
+ if !cs[ l] . is_ascii_alphabetic ( ) {
9
+ l += 1 ;
10
+ } else if !cs[ r] . is_ascii_alphabetic ( ) {
11
+ r -= 1 ;
12
+ } else {
13
+ cs. swap ( l, r) ;
14
+ l += 1 ;
15
+ r -= 1 ;
16
+ }
17
+ }
18
+ cs. iter ( ) . collect ( )
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments