Skip to content

Commit 0234107

Browse files
committedSep 25, 2021
Add solution #6
1 parent 59e4cf8 commit 0234107

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
2|[Add Two Numbers](./0002-add-two-numbers.js)|Medium|
1111
3|[Longest Substring Without Repeating Characters](./0003-longest-substring-without-repeating-characters.js)|Medium|
1212
4|[Median of Two Sorted Arrays](./0004-median-of-two-sorted-arrays.js)|Hard|
13+
6|[ZigZag Conversion](./0006-zigzag-conversion.js)|Medium|
1314
7|[Reverse Integer](./0007-reverse-integer.js)|Easy|
1415
8|[String to Integer (atoi)](./0008-string-to-integer-atoi.js)|Medium|
1516
10|[Regular Expression Matching](./0010-regular-expression-matching.js)|Hard|

‎solutions/0006-zigzag-conversion.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 6. ZigZag Conversion
3+
* https://leetcode.com/problems/zigzag-conversion/
4+
* Difficulty: Medium
5+
*
6+
* The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given
7+
* number of rows like this: (you may want to display this pattern in a
8+
* fixed font for better legibility)
9+
*
10+
* > P A H N
11+
* > A P L S I I G
12+
* > Y I R
13+
*
14+
* And then read line by line: `"PAHNAPLSIIGYIR"`
15+
*/
16+
17+
/**
18+
* @param {string} s
19+
* @param {number} numRows
20+
* @return {string}
21+
*/
22+
var convert = function(s, numRows) {
23+
const order = [...new Array(numRows).keys()];
24+
order.push(...order.slice(1, -1).reverse());
25+
26+
const rows = new Array(numRows).fill('');
27+
[...s].forEach((c, i) => (rows[order[i % order.length]] += c));
28+
29+
return rows.join('');
30+
};

0 commit comments

Comments
 (0)
Please sign in to comment.