File tree Expand file tree Collapse file tree 4 files changed +161
-0
lines changed
solution/0000-0099/0006.ZigZag Conversion Expand file tree Collapse file tree 4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,63 @@ const s = 'AB',
266
266
console .log (convert (s, numRows));
267
267
```
268
268
269
+ ### ** TypeScript**
270
+
271
+ ``` ts
272
+ function convert(s : string , numRows : number ): string {
273
+ if (numRows === 1 ) {
274
+ return s ;
275
+ }
276
+ const ss = new Array (numRows ).fill (' ' );
277
+ let i = 0 ;
278
+ let toDown = true ;
279
+ for (const c of s ) {
280
+ ss [i ] += c ;
281
+ if (toDown ) {
282
+ i ++ ;
283
+ } else {
284
+ i -- ;
285
+ }
286
+ if (i === 0 || i === numRows - 1 ) {
287
+ toDown = ! toDown ;
288
+ }
289
+ }
290
+ return ss .reduce ((r , s ) => r + s );
291
+ }
292
+ ```
293
+
294
+ ### ** Rust**
295
+
296
+ ``` rust
297
+ impl Solution {
298
+ pub fn convert (s : String , num_rows : i32 ) -> String {
299
+ let num_rows = num_rows as usize ;
300
+ if num_rows == 1 {
301
+ return s ;
302
+ }
303
+ let mut ss = vec! [String :: new (); num_rows ];
304
+ let mut i = 0 ;
305
+ let mut to_down = true ;
306
+ for c in s . chars () {
307
+ ss [i ]. push (c );
308
+ if to_down {
309
+ i += 1 ;
310
+ } else {
311
+ i -= 1 ;
312
+ }
313
+ if i == 0 || i == num_rows - 1 {
314
+ to_down = ! to_down ;
315
+ }
316
+ }
317
+ let mut res = String :: new ();
318
+ for i in 0 .. num_rows {
319
+ res += & ss [i ];
320
+ }
321
+ res
322
+ }
323
+ }
324
+ ```
325
+
269
326
### ** ...**
270
327
271
328
```
Original file line number Diff line number Diff line change @@ -260,6 +260,63 @@ const s = 'AB',
260
260
console .log (convert (s, numRows));
261
261
```
262
262
263
+ ### ** TypeScript**
264
+
265
+ ``` ts
266
+ function convert(s : string , numRows : number ): string {
267
+ if (numRows === 1 ) {
268
+ return s ;
269
+ }
270
+ const ss = new Array (numRows ).fill (' ' );
271
+ let i = 0 ;
272
+ let toDown = true ;
273
+ for (const c of s ) {
274
+ ss [i ] += c ;
275
+ if (toDown ) {
276
+ i ++ ;
277
+ } else {
278
+ i -- ;
279
+ }
280
+ if (i === 0 || i === numRows - 1 ) {
281
+ toDown = ! toDown ;
282
+ }
283
+ }
284
+ return ss .reduce ((r , s ) => r + s );
285
+ }
286
+ ```
287
+
288
+ ### ** Rust**
289
+
290
+ ``` rust
291
+ impl Solution {
292
+ pub fn convert (s : String , num_rows : i32 ) -> String {
293
+ let num_rows = num_rows as usize ;
294
+ if num_rows == 1 {
295
+ return s ;
296
+ }
297
+ let mut ss = vec! [String :: new (); num_rows ];
298
+ let mut i = 0 ;
299
+ let mut to_down = true ;
300
+ for c in s . chars () {
301
+ ss [i ]. push (c );
302
+ if to_down {
303
+ i += 1 ;
304
+ } else {
305
+ i -= 1 ;
306
+ }
307
+ if i == 0 || i == num_rows - 1 {
308
+ to_down = ! to_down ;
309
+ }
310
+ }
311
+ let mut res = String :: new ();
312
+ for i in 0 .. num_rows {
313
+ res += & ss [i ];
314
+ }
315
+ res
316
+ }
317
+ }
318
+ ```
319
+
263
320
### ** ...**
264
321
265
322
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn convert ( s : String , num_rows : i32 ) -> String {
3
+ let num_rows = num_rows as usize ;
4
+ if num_rows == 1 {
5
+ return s;
6
+ }
7
+ let mut ss = vec ! [ String :: new( ) ; num_rows] ;
8
+ let mut i = 0 ;
9
+ let mut to_down = true ;
10
+ for c in s. chars ( ) {
11
+ ss[ i] . push ( c) ;
12
+ if to_down {
13
+ i += 1 ;
14
+ } else {
15
+ i -= 1 ;
16
+ }
17
+ if i == 0 || i == num_rows - 1 {
18
+ to_down = !to_down;
19
+ }
20
+ }
21
+ let mut res = String :: new ( ) ;
22
+ for i in 0 ..num_rows {
23
+ res += & ss[ i] ;
24
+ }
25
+ res
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ function convert ( s : string , numRows : number ) : string {
2
+ if ( numRows === 1 ) {
3
+ return s ;
4
+ }
5
+ const ss = new Array ( numRows ) . fill ( '' ) ;
6
+ let i = 0 ;
7
+ let toDown = true ;
8
+ for ( const c of s ) {
9
+ ss [ i ] += c ;
10
+ if ( toDown ) {
11
+ i ++ ;
12
+ } else {
13
+ i -- ;
14
+ }
15
+ if ( i === 0 || i === numRows - 1 ) {
16
+ toDown = ! toDown ;
17
+ }
18
+ }
19
+ return ss . reduce ( ( r , s ) => r + s ) ;
20
+ }
You can’t perform that action at this time.
0 commit comments