-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
53 lines (50 loc) · 1.18 KB
/
solution.js
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
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function (s, numRows) {
var arrs = Array.apply(null, {
length: numRows,
}).map(function () {
return [];
});
var x = 0;
var y = 0;
var direction = 1;
var strLen = s.length;
var maxRow = numRows - 1;
var item;
for (var i = 0; i < strLen; i++) {
item = s[i];
arrs[x][y] = item;
// 推算下一个的坐标
if (x === maxRow) {
direction = -1;
y++;
// 处理只有一行的情况
if (x > 0) {
x--;
}
} else if (x === 0 && direction === -1) {
direction = 1;
x++;
} else {
x += direction;
if (direction === -1) {
y++;
}
}
}
var strArr = arrs.reduce(function (arr, arrItem) {
var fragment = arrItem.reduce(function (arr2, item) {
if (item) {
arr2.push(item);
}
return arr2;
}, []).join('');
arr.push(fragment);
return arr;
}, []);
return strArr.join('');
};