Javascript with step up&down


  • 0
    S

    Get references from different posts.

      
    

    /** * @param {string} s * @param {number} numRows * @return {string} */ var convert = function(s, numRows) { if (s.length < 2 || numRows === 1) return s; // ES6 to prefill "" for each row. let rows = new Array(numRows).fill(""); // step = 1 to step down to numRows - 1; step = -1 to step up to 0. let step = 1; let idx = 0; for(let i = 0; i < s.length; i++) { rows[idx] += s[i]; if (idx === numRows - 1) step = -1; if (idx === 0) step = 1; idx += step; } return rows.join(""); };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.