Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 3x 1x 2x 2x 2x 2x 7x 21x 21x 7x 2x | export default function convert(s: string, numRows: number): string {
if (numRows === 1)
return s
const n = s.length
const cycleLen = 2 * numRows - 2
let ret = ''
// 行 0 中的字符位于索引 k * (2 * numRows - 2).
// 行 numRows - 1 中的字符位于索引 k * (2 * numRows - 2) + numRows - 1.
// 中间 i 行中的字符位于 k * (2 * numRows - 2) + i 以及 (k + 1) * (2 * numRows - 2) - i.
for (let i = 0; i < numRows; i++) {
for (let j = 0; j + i < n; j += cycleLen) {
ret += s[j + i]
if (i !== 0 && i !== numRows - 1 && j + cycleLen - i < n)
ret += s[j + cycleLen - i]
}
}
return ret
}
|