Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6d858fc

Browse files
✨ zigzag conversion
1 parent 15c9190 commit 6d858fc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎src/6-zigzag-conversion/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
y++;
35+
} else {
36+
x++;
37+
y--;
38+
}
39+
40+
continue;
41+
}
42+
}
43+
44+
return res.reduce((acc, x) => acc + x, '');
45+
};
46+
convert('AB', 1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /