-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0006_Zigzag_Conversion.js
35 lines (31 loc) · 1.18 KB
/
0006_Zigzag_Conversion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function(s, numRows) {
const len = s.length;
// no zigzag pattern for these cases
if (numRows < 2 || len < numRows) {
return s;
}
// create Array object of length numRows and fill with ''
// rows[0]: first row characters, rows[1]: second row characters...
const rows = new Array(numRows).fill('');
let reverse = false;
let count = 0;
// save characters in the correct rows, find the correct rows by count
// if reach the end or start of the rows, change reverse value
// rows[0] = A E
// rows[1] = B D F
// rows[2] = C
for (let i = 0; i < len; i++) {
rows[count] += s[i]; // save characters in the correct rows
reverse? count-- : count++; // go down or up to the next rows
if (count === numRows - 1 || count === 0) { // if reach the end or start of the rows,
reverse = !reverse; // change reverse value
}
}
// rows.join('') = AEBDFC
return rows.join('');
};