less than 1 minute read

Problem

Well, I don’t think this is a good question. Downvote is twice as much as upvote.

I didn’t solve this problem personally. But I’ll share a good solution. Please check the description at the link.

Solution

https://leetcode.com/problems/zigzag-conversion/solutions/3522/intuitive-javascript-solution/

var convert = function(s, numRows) {
    // return original string if can't zigzag
    if (numRows === 1 || s.length < numRows) return s;

    let rows = []
    let converted = '';
    let reverse = false;
    let count = 0

    // prepare rows
    for (let i = 0; i < numRows; i++) rows[i] = [];
    // reverse the push flow when reaching turning points
    for (let i = 0; i < s.length; i++) {
        rows[count].push(s[i]);
        reverse ? count-- : count++;
        if (count === numRows - 1 || count === 0) reverse = !reverse;
    }
    // put together converted string
    return rows.reduce((converted, cur) => converted + cur.join(''), '');
};

Leave a comment