6.js
/** ------------------------------------------------------------------------------
*
* 6. Zigzag Conversion
* Topics: String
* https://leetcode.com/problems/zigzag-conversion/description/
*
------------------------------------------------------------------------------ */
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function (s, numRows) {
if (numRows === 1 || numRows >= s.length) return s
const rows = Array.from({ length: numRows }, () => "")
let curRow = 0
let goingDown = false
for (const char of s) {
rows[curRow] += char
if (curRow === 0 || curRow === numRows - 1) goingDown = !goingDown
curRow += goingDown ? 1 : -1
}
return rows.join("")
}
console.log(convert("PAYPALISHIRING", 3))
console.log(convert("PAYPALISHIRING", 4))
console.log(convert("A", 1))