File tree 2 files changed +31
-0
lines changed 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 10
10
2|[ Add Two Numbers] ( ./0002-add-two-numbers.js ) |Medium|
11
11
3|[ Longest Substring Without Repeating Characters] ( ./0003-longest-substring-without-repeating-characters.js ) |Medium|
12
12
4|[ Median of Two Sorted Arrays] ( ./0004-median-of-two-sorted-arrays.js ) |Hard|
13
+ 6|[ ZigZag Conversion] ( ./0006-zigzag-conversion.js ) |Medium|
13
14
7|[ Reverse Integer] ( ./0007-reverse-integer.js ) |Easy|
14
15
8|[ String to Integer (atoi)] ( ./0008-string-to-integer-atoi.js ) |Medium|
15
16
10|[ Regular Expression Matching] ( ./0010-regular-expression-matching.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments