|
78 | 78 |
|
79 | 79 | ### 方法一:模拟
|
80 | 80 |
|
81 |
| -我们用一个二维数组 $g$ 来模拟 $Z$ 字形排列的过程,其中 $g[i][j]$ 表示第 $i$ 行第 $j$ 列的字符。初始时 $i=0$,另外我们定义一个方向变量 $k$,初始时 $k=-1$,表示向上走。 |
| 81 | +我们用一个二维数组 $g$ 来模拟 Z 字形排列的过程,其中 $g[i][j]$ 表示第 $i$ 行第 $j$ 列的字符。初始时 $i = 0$,另外我们定义一个方向变量 $k$,初始时 $k = -1$,表示向上走。 |
82 | 82 |
|
83 |
| -我们从左到右遍历字符串 $s$,每次遍历到一个字符 $c$,将其追加到 $g[i]$ 中,如果此时 $i=0$ 或者 $i=numRows-1$,说明当前字符位于 $Z$ 字形排列的拐点,我们将 $k$ 的值反转,即 $k=-k$。接下来,我们将 $i$ 的值更新为 $i+k$,即向上或向下移动一行。继续遍历下一个字符,直到遍历完字符串 $s$,我们返回 $g$ 中所有行拼接后的字符串即可。 |
| 83 | +我们从左到右遍历字符串 $s$,每次遍历到一个字符 $c$,将其追加到 $g[i]$ 中。如果此时 $i = 0$ 或者 $i = \textit{numRows} - 1$,说明当前字符位于 Z 字形排列的拐点,我们将 $k$ 的值反转,即 $k = -k$。接下来,我们将 $i$ 的值更新为 $i + k$,即向上或向下移动一行。继续遍历下一个字符,直到遍历完字符串 $s$,我们返回 $g$ 中所有行拼接后的字符串即可。 |
84 | 84 |
|
85 | 85 | 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
|
86 | 86 |
|
@@ -199,29 +199,24 @@ function convert(s: string, numRows: number): string {
|
199 | 199 | ```rust
|
200 | 200 | impl Solution {
|
201 | 201 | pub fn convert(s: String, num_rows: i32) -> String {
|
202 |
| - let num_rows = num_rows as usize; |
203 | 202 | if num_rows == 1 {
|
204 | 203 | return s;
|
205 | 204 | }
|
206 |
| - let mut ss = vec![String::new(); num_rows]; |
| 205 | + |
| 206 | + let num_rows = num_rows as usize; |
| 207 | + let mut g = vec![String::new(); num_rows]; |
207 | 208 | let mut i = 0;
|
208 |
| - let mut to_down = true; |
| 209 | + let mut k = -1; |
| 210 | + |
209 | 211 | for c in s.chars() {
|
210 |
| - ss[i].push(c); |
211 |
| - if to_down { |
212 |
| - i += 1; |
213 |
| - } else { |
214 |
| - i -= 1; |
215 |
| - } |
| 212 | + g[i].push(c); |
216 | 213 | if i == 0 || i == num_rows - 1 {
|
217 |
| - to_down = !to_down; |
| 214 | + k = -k; |
218 | 215 | }
|
| 216 | + i = (i as isize + k) as usize; |
219 | 217 | }
|
220 |
| - let mut res = String::new(); |
221 |
| - for i in 0..num_rows { |
222 |
| - res += &ss[i]; |
223 |
| - } |
224 |
| - res |
| 218 | + |
| 219 | + g.concat() |
225 | 220 | }
|
226 | 221 | }
|
227 | 222 | ```
|
@@ -323,220 +318,42 @@ char* convert(char* s, int numRows) {
|
323 | 318 | }
|
324 | 319 | ```
|
325 | 320 |
|
326 |
| -<!-- tabs:end --> |
327 |
| -
|
328 |
| -<!-- solution:end --> |
329 |
| -
|
330 |
| -<!-- solution:start --> |
331 |
| -
|
332 |
| -### 方法二 |
333 |
| -
|
334 |
| -<!-- tabs:start --> |
335 |
| -
|
336 |
| -#### Python3 |
337 |
| -
|
338 |
| -```python |
339 |
| -class Solution: |
340 |
| - def convert(self, s: str, numRows: int) -> str: |
341 |
| - if numRows == 1: |
342 |
| - return s |
343 |
| - group = 2 * numRows - 2 |
344 |
| - ans = [] |
345 |
| - for i in range(1, numRows + 1): |
346 |
| - interval = group if i == numRows else 2 * numRows - 2 * i |
347 |
| - idx = i - 1 |
348 |
| - while idx < len(s): |
349 |
| - ans.append(s[idx]) |
350 |
| - idx += interval |
351 |
| - interval = group - interval |
352 |
| - if interval == 0: |
353 |
| - interval = group |
354 |
| - return ''.join(ans) |
355 |
| -``` |
356 |
| - |
357 |
| -#### Java |
358 |
| - |
359 |
| -```java |
360 |
| -class Solution { |
361 |
| - public String convert(String s, int numRows) { |
362 |
| - if (numRows == 1) { |
363 |
| - return s; |
364 |
| - } |
365 |
| - StringBuilder ans = new StringBuilder(); |
366 |
| - int group = 2 * numRows - 2; |
367 |
| - for (int i = 1; i <= numRows; i++) { |
368 |
| - int interval = i == numRows ? group : 2 * numRows - 2 * i; |
369 |
| - int idx = i - 1; |
370 |
| - while (idx < s.length()) { |
371 |
| - ans.append(s.charAt(idx)); |
372 |
| - idx += interval; |
373 |
| - interval = group - interval; |
374 |
| - if (interval == 0) { |
375 |
| - interval = group; |
376 |
| - } |
377 |
| - } |
378 |
| - } |
379 |
| - return ans.toString(); |
380 |
| - } |
381 |
| -} |
382 |
| -``` |
383 |
| - |
384 |
| -#### C++ |
385 |
| - |
386 |
| -```cpp |
387 |
| -class Solution { |
388 |
| -public: |
389 |
| - string convert(string s, int numRows) { |
390 |
| - if (numRows == 1) return s; |
391 |
| - string ans; |
392 |
| - int group = 2 * numRows - 2; |
393 |
| - for (int i = 1; i <= numRows; ++i) { |
394 |
| - int interval = i == numRows ? group : 2 * numRows - 2 * i; |
395 |
| - int idx = i - 1; |
396 |
| - while (idx < s.length()) { |
397 |
| - ans.push_back(s[idx]); |
398 |
| - idx += interval; |
399 |
| - interval = group - interval; |
400 |
| - if (interval == 0) interval = group; |
401 |
| - } |
402 |
| - } |
403 |
| - return ans; |
404 |
| - } |
405 |
| -}; |
406 |
| -``` |
407 |
| -
|
408 |
| -#### Go |
409 |
| -
|
410 |
| -```go |
411 |
| -func convert(s string, numRows int) string { |
412 |
| - if numRows == 1 { |
413 |
| - return s |
414 |
| - } |
415 |
| - n := len(s) |
416 |
| - ans := make([]byte, n) |
417 |
| - step := 2*numRows - 2 |
418 |
| - count := 0 |
419 |
| - for i := 0; i < numRows; i++ { |
420 |
| - for j := 0; j+i < n; j += step { |
421 |
| - ans[count] = s[i+j] |
422 |
| - count++ |
423 |
| - if i != 0 && i != numRows-1 && j+step-i < n { |
424 |
| - ans[count] = s[j+step-i] |
425 |
| - count++ |
426 |
| - } |
427 |
| - } |
428 |
| - } |
429 |
| - return string(ans) |
430 |
| -} |
431 |
| -``` |
432 |
| - |
433 |
| -#### TypeScript |
434 |
| - |
435 |
| -```ts |
436 |
| -function convert(s: string, numRows: number): string { |
437 |
| - if (numRows === 1) { |
438 |
| - return s; |
439 |
| - } |
440 |
| - const ss = new Array(numRows).fill(''); |
441 |
| - let i = 0; |
442 |
| - let toDown = true; |
443 |
| - for (const c of s) { |
444 |
| - ss[i] += c; |
445 |
| - if (toDown) { |
446 |
| - i++; |
447 |
| - } else { |
448 |
| - i--; |
449 |
| - } |
450 |
| - if (i === 0 || i === numRows - 1) { |
451 |
| - toDown = !toDown; |
452 |
| - } |
453 |
| - } |
454 |
| - return ss.reduce((r, s) => r + s); |
455 |
| -} |
456 |
| -``` |
457 |
| - |
458 |
| -#### Rust |
459 |
| - |
460 |
| -```rust |
461 |
| -impl Solution { |
462 |
| - pub fn convert(s: String, num_rows: i32) -> String { |
463 |
| - let num_rows = num_rows as usize; |
464 |
| - let mut rows = vec![String::new(); num_rows]; |
465 |
| - let iter = (0..num_rows).chain((1..num_rows - 1).rev()).cycle(); |
466 |
| - iter.zip(s.chars()).for_each(|(i, c)| rows[i].push(c)); |
467 |
| - rows.into_iter().collect() |
468 |
| - } |
469 |
| -} |
470 |
| -``` |
471 |
| - |
472 |
| -#### JavaScript |
473 |
| - |
474 |
| -```js |
475 |
| -/** |
476 |
| - * @param {string} s |
477 |
| - * @param {number} numRows |
478 |
| - * @return {string} |
479 |
| - */ |
480 |
| -var convert = function (s, numRows) { |
481 |
| - if (numRows == 1) return s; |
482 |
| - const arr = new Array(numRows); |
483 |
| - for (let i = 0; i < numRows; i++) arr[i] = []; |
484 |
| - let mi = 0, |
485 |
| - isDown = true; |
486 |
| - for (const c of s) { |
487 |
| - arr[mi].push(c); |
488 |
| - |
489 |
| - if (mi >= numRows - 1) isDown = false; |
490 |
| - else if (mi <= 0) isDown = true; |
491 |
| - |
492 |
| - if (isDown) mi++; |
493 |
| - else mi--; |
494 |
| - } |
495 |
| - let ans = []; |
496 |
| - for (const item of arr) { |
497 |
| - ans = ans.concat(item); |
498 |
| - } |
499 |
| - return ans.join(''); |
500 |
| -}; |
501 |
| -``` |
502 |
| - |
503 | 321 | #### PHP
|
504 | 322 |
|
505 | 323 | ```php
|
506 | 324 | class Solution {
|
507 | 325 | /**
|
508 |
| - * @param string $s |
509 |
| - * @param int $numRows |
510 |
| - * @return string |
| 326 | + * @param String $s |
| 327 | + * @param Integer $numRows |
| 328 | + * @return String |
511 | 329 | */
|
512 |
| - |
513 | 330 | function convert($s, $numRows) {
|
514 |
| - if ($numRows == 1 || strlen($s) <= $numRows) { |
| 331 | + if ($numRows == 1) { |
515 | 332 | return $s;
|
516 | 333 | }
|
517 | 334 |
|
518 |
| - $result = ''; |
519 |
| - $cycleLength = 2 * $numRows - 2; |
520 |
| - $n = strlen($s); |
| 335 | + $g = array_fill(0, $numRows, ''); |
| 336 | + $i = 0; |
| 337 | + $k = -1; |
521 | 338 |
|
522 |
| - for ($i = 0; $i < $numRows; $i++) { |
523 |
| - for ($j = 0; $j + $i < $n; $j += $cycleLength) { |
524 |
| - $result .= $s[$j + $i]; |
| 339 | + $length = strlen($s); |
| 340 | + for ($j = 0; $j < $length; $j++) { |
| 341 | + $c = $s[$j]; |
| 342 | + $g[$i] .= $c; |
525 | 343 |
|
526 |
| - if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) { |
527 |
| - $result .= $s[$j + $cycleLength - $i]; |
528 |
| - } |
| 344 | + if ($i == 0 || $i == $numRows - 1) { |
| 345 | + $k = -$k; |
529 | 346 | }
|
530 |
| - } |
531 | 347 |
|
532 |
| - return $result; |
| 348 | + $i += $k; |
| 349 | + } |
| 350 | + return implode('', $g); |
533 | 351 | }
|
534 | 352 | }
|
535 |
| -`` |
| 353 | +``` |
536 | 354 |
|
537 | 355 | <!-- tabs:end -->
|
538 | 356 |
|
539 | 357 | <!-- solution:end -->
|
540 | 358 |
|
541 | 359 | <!-- problem:end -->
|
542 |
| -``` |
0 commit comments