JS solution


  • 0
    H
     * @param {string} s
     * @param {number} numRows
     * @return {string}
     */
    var convert = function(s, numRows) {
      let index = 0;
      let step = 1;
      let result = [];
      if (s.length <= 1 || numRows >= s.length) {
        return s;
      }
    
      for (let i = 0; i < s.length; i++) {
        if (result[index] === undefined) {
          result[index] = '';
        }
        result[index] += s[i];
        if (index === 0) {
          step = 1;
        } else if (index === numRows - 1) {
          step = -1;
        }
        index += step;
      }
      console.log(result);
      return result.join('');
    };```

Log in to reply
 

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