-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006_zigzag_converstion.js
More file actions
84 lines (77 loc) · 2.29 KB
/
006_zigzag_converstion.js
File metadata and controls
84 lines (77 loc) · 2.29 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
// const convert = function(s, numRows) {
// const sArr = s.split('');
// let cols = {};
// let col = 1;
// let index = numRows-2;
// while (sArr.length !== 0) {
//
// if (col === 1 || col%(numRows-1) === 1) {
// if (col !==1) {
// index = numRows -2;
// }
// if (sArr.length <= numRows) {
// cols[col] = sArr.splice(0, sArr.length);
// for (let j=cols[col].length; j<numRows; j++) {
// console.log(j);
// cols[col][j] = ' ';
// }
// console.log(cols[col]);
// } else {
// if (numRows<=2) {
// cols[col] = sArr.splice(0, 1);
// } else {
// cols[col] = sArr.splice(0, numRows);
// }
// }
// } else {
// cols[col] = new Array(numRows).fill(' ');
// cols[col][index] = sArr.splice(0, 1)[0];
// index--;
// }
//
// col ++;
//
// }
// let result = '';
// for (let i = 0; i< numRows; i++) {
// Object.keys(cols).forEach(function(key) {
// console.log(cols[key]);
// if (cols[key][i] !== ' ') {
// result +=cols[key][i];
// }
// });
// }
// return numRows === 1 ? s : result;
// };
const convert = function (s, numRows) {
if(s.length < numRows || numRows < 2 || s.length < 2) {
return s;
}
let returnS = '';
// Loop over rows
for (let j = 0; j < numRows; j++) {
for (let i = j; i < s.length; i += 2 * (numRows - 1)) {
returnS += s[i];
console.log(s[i]);
if (j !== 0 && j < numRows - 1) {
/**
* Not first row and last row
* @type {number}
*/
const intermediateIndex = i + 2 * (numRows - j - 1);
if (s[intermediateIndex]) {
console.log(s[intermediateIndex]);
returnS += s[intermediateIndex];
}
}
}
}
return returnS;
};
const a = 'PAYPALISHIRING';
console.log(convert(a, 4));