All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0006_zigzag_conversion.solution.js Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #String #2024_11_17_Time_2_ms_(99.71%)_Space_44.5_MB_(94.69%)

/**
 * @param {string} s
 * @param {number} numRows
 * @return {string}
 */
var convert = function (s, numRows) {
    const sLen = s.length
    if (numRows === 1) {
        return s
    }
    const maxDist = numRows * 2 - 2
    let result = ''

    for (let i = 0; i < numRows; i++) {
        let index = i
        if (i === 0 || i === numRows - 1) {
            while (index < sLen) {
                result += s[index]
                index += maxDist
            }
        } else {
            while (index < sLen) {
                result += s[index]
                index += maxDist - i * 2
                if (index >= sLen) {
                    break
                }
                result += s[index]
                index += i * 2
            }
        }
    }
    return result
}

export { convert }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy