-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
65 lines (65 loc) · 1.55 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @param {string} s
* @param {number} numRows
* @return {string}
*/
//Distribution 95.33%,runtime 176ms
var convert = function(s, numRows) {
var res = "";
if (numRows === 1) return s;
for (var i = 0; i < numRows; ++i){
var j = i;
while (j < s.length){
res += s[j];
if (i !== 0 && i !== numRows - 1 && j + (numRows - i) * 2 - 2 < s.length)
res += s[j + (numRows - i) * 2 - 2];
j += (numRows - 1) * 2;
}
}
return res;
};
//Distribution 12.15%,runtime 244ms
var convert = function(s, numRows) {
var i, j;
if(numRows === 1){
return s;
}
var count = 0;
var colDirect = true;
var row = 0, col = 0;
var map = [];
for(i = 0; i < s.length; i++){
map[i] = [];
}
for(i = 0; i < s.length; i++){
if(colDirect){
map[row][col] = s[i];
row++;
count++;
}else{
map[row][col] = s[i];
row--;
col++;
count++;
}
if(colDirect && count === numRows){
count = 0;
row -= 2;
col++;
colDirect = false;
}
if(!colDirect && count === numRows - 2){
count = 0;
colDirect = true;
}
}
var res = "";
for(i = 0; i < map.length; i++){
for(j = 0; j < map[i].length; j++){
if(map[i][j]){
res += map[i][j];
}
}
}
return res;
};