File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
solution/1600-1699/1694.Reformat Phone Number Expand file tree Collapse file tree 4 files changed +110
-0
lines changed Original file line number Diff line number Diff line change @@ -198,6 +198,46 @@ func reformatNumber(number string) string {
198
198
}
199
199
```
200
200
201
+ ### ** TypeScript**
202
+
203
+ ``` ts
204
+ function reformatNumber(number : string ): string {
205
+ const cs = [... number ].filter (c => c !== ' ' && c !== ' -' );
206
+ const n = cs .length ;
207
+ return cs
208
+ .map ((v , i ) => {
209
+ if (
210
+ ((i + 1 ) % 3 === 0 && i < n - 2 ) ||
211
+ (n % 3 === 1 && n - 3 === i )
212
+ ) {
213
+ return v + ' -' ;
214
+ }
215
+ return v ;
216
+ })
217
+ .join (' ' );
218
+ }
219
+ ```
220
+
221
+ ### ** Rust**
222
+
223
+ ``` rust
224
+ impl Solution {
225
+ pub fn reformat_number (number : String ) -> String {
226
+ let cs : Vec <char > = number . chars (). filter (| & c | c != ' ' && c != '-' ). collect ();
227
+ let n = cs . len ();
228
+ cs . iter ()
229
+ . enumerate ()
230
+ . map (| (i , c )| {
231
+ if (i + 1 ) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
232
+ return c . to_string () + & " -" ;
233
+ }
234
+ c . to_string ()
235
+ })
236
+ . collect ()
237
+ }
238
+ }
239
+ ```
240
+
201
241
### ** ...**
202
242
203
243
```
Original file line number Diff line number Diff line change @@ -159,6 +159,46 @@ func reformatNumber(number string) string {
159
159
}
160
160
```
161
161
162
+ ### ** TypeScript**
163
+
164
+ ``` ts
165
+ function reformatNumber(number : string ): string {
166
+ const cs = [... number ].filter (c => c !== ' ' && c !== ' -' );
167
+ const n = cs .length ;
168
+ return cs
169
+ .map ((v , i ) => {
170
+ if (
171
+ ((i + 1 ) % 3 === 0 && i < n - 2 ) ||
172
+ (n % 3 === 1 && n - 3 === i )
173
+ ) {
174
+ return v + ' -' ;
175
+ }
176
+ return v ;
177
+ })
178
+ .join (' ' );
179
+ }
180
+ ```
181
+
182
+ ### ** Rust**
183
+
184
+ ``` rust
185
+ impl Solution {
186
+ pub fn reformat_number (number : String ) -> String {
187
+ let cs : Vec <char > = number . chars (). filter (| & c | c != ' ' && c != '-' ). collect ();
188
+ let n = cs . len ();
189
+ cs . iter ()
190
+ . enumerate ()
191
+ . map (| (i , c )| {
192
+ if (i + 1 ) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
193
+ return c . to_string () + & " -" ;
194
+ }
195
+ c . to_string ()
196
+ })
197
+ . collect ()
198
+ }
199
+ }
200
+ ```
201
+
162
202
### ** ...**
163
203
164
204
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn reformat_number ( number : String ) -> String {
3
+ let cs: Vec < char > = number. chars ( ) . filter ( |& c| c != ' ' && c != '-' ) . collect ( ) ;
4
+ let n = cs. len ( ) ;
5
+ cs. iter ( )
6
+ . enumerate ( )
7
+ . map ( |( i, c) | {
8
+ if ( i + 1 ) % 3 == 0 && i < n - 2 || n % 3 == 1 && i == n - 3 {
9
+ return c. to_string ( ) + & "-" ;
10
+ }
11
+ c. to_string ( )
12
+ } )
13
+ . collect ( )
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ function reformatNumber ( number : string ) : string {
2
+ const cs = [ ...number ] . filter ( c => c !== ' ' && c !== '-' ) ;
3
+ const n = cs . length ;
4
+ return cs
5
+ . map ( ( v , i ) => {
6
+ if (
7
+ ( ( i + 1 ) % 3 === 0 && i < n - 2 ) ||
8
+ ( n % 3 === 1 && n - 3 === i )
9
+ ) {
10
+ return v + '-' ;
11
+ }
12
+ return v ;
13
+ } )
14
+ . join ( '' ) ;
15
+ }
You can’t perform that action at this time.
0 commit comments