GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #6 - ZigZag Conversion - LeetCode JavaScript Solutions

Back to all solutions

#6 - ZigZag Conversion

Medium • View on LeetCode • View on GitHub
String

Problem Description

The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) > P A H N > A P L S I I G > Y I R And then read line by line: `"PAHNAPLSIIGYIR"`

Solution

/**
 * @param {string} s
 * @param {number} numRows
 * @return {string}
 */
var convert = function(s, numRows) {
  const order = [...new Array(numRows).keys()];
  order.push(...order.slice(1, -1).reverse());

  const rows = new Array(numRows).fill('');
  [...s].forEach((c, i) => (rows[order[i % order.length]] += c));

  return rows.join('');
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.