-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigzagConversation.js
More file actions
39 lines (32 loc) · 1.02 KB
/
Copy pathZigzagConversation.js
File metadata and controls
39 lines (32 loc) · 1.02 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
"use strict"
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
/* Results
Runtime: 110 ms, faster than 63.67% of JavaScript online submissions for Zigzag Conversion.
Memory Usage: 48.6 MB, less than 45.21% of JavaScript online submissions for Zigzag Conversion.
*/
var convert = function (s, numRows) {
if (numRows === 1)
return s;
let arr = new Array();
for (let x = 0; x < numRows; x++)
arr.push(new Array());
for (let stringIndex = 0, arrayIndex = 0, offset = 1; stringIndex < s.length; stringIndex++) {
arr[arrayIndex].push(s[stringIndex]);
if (arrayIndex === 0)
offset = 1;
if (arrayIndex === numRows - 1)
offset = -1;
arrayIndex = arrayIndex + offset;
}
arr = arr.flat();
return arr.join("").toString();
};
(function test() {
console.log("AB " + convert("AB", 1));
console.log("PAHNAPLSIIGYIR " + convert("PAYPALISHIRING", 3));
console.log("PINALSIGYAHRPI " + convert("PAYPALISHIRING", 4));
})()