1 var convert = function(s, numRows) {
2 if (numRows === 1) {
3 return s;
4 }
5
6 var i, j,
7 count = 0,
8 colDirect = true,
9 row = 0,
10 col = 0,
11 map = [];
12
13 for (i = 0; i < s.length; i++) {
14 map[i] = [];
15 }
16
17 for (i = 0; i < s.length; i++) {
18 map[row][col] = s[i];
19
20 if (colDirect) {
21 row++;
22 } else {
23 row--;
24 col++;
25 }
26
27 count++;
28
29 if (colDirect && count === numRows) {
30 row -= 2;
31 col++;
32 count = 0;
33 colDirect = false;
34 }
35
36 if (!colDirect && count === numRows - 2) {
37 count = 0;
38 colDirect = true;
39 }
40 }
41
42 var res = "";
43 for (i = 0; i < map.length; i++) {
44 for (j = 0; j < map[i].length; j++) {
45 if (map[i][j]) {
46 res += map[i][j];
47 }
48 }
49 }
50
51 return res;
52 };