We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 15c9190 commit 6d858fcCopy full SHA for 6d858fc
src/6-zigzag-conversion/index.js
@@ -0,0 +1,46 @@
1
+/**
2
+ * @param {string} s
3
+ * @param {number} numRows
4
+ * @return {string}
5
+ */
6
+var convert = function (s, numRows) {
7
+ if (numRows === 1) return s;
8
+ const res = [];
9
+ let x = 0;
10
+ let y = 0;
11
+ let direction = 'DOWN';
12
+
13
+ for (let i = 0; i < s.length; i++) {
14
+ if (!res[y]) {
15
+ res[y] = '';
16
+ }
17
+ res[y] += s[i];
18
19
+ if (direction === 'DOWN') {
20
+ if (y + 1 === numRows) {
21
+ x++;
22
+ y--;
23
+ direction = 'RIGHT_UP';
24
+ } else {
25
+ y++;
26
27
28
+ continue;
29
30
31
+ if (direction === 'RIGHT_UP') {
32
+ if (y === 0) {
33
+ direction = 'DOWN';
34
35
36
37
38
39
40
41
42
43
44
+ return res.reduce((acc, x) => acc + x, '');
45
+};
46
+convert('AB', 1);
0 commit comments