-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2624-snail-traversal.ts
More file actions
57 lines (54 loc) · 1.33 KB
/
Copy path2624-snail-traversal.ts
File metadata and controls
57 lines (54 loc) · 1.33 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export function snailTool(data: number[], rowsCount: number, colsCount: number): number[][] {
if (data.length !== rowsCount * colsCount) {
return [];
}
const cols = [];
for (let i = 0; i < colsCount; i++) {
cols.push(data.slice(rowsCount * i, rowsCount * i + rowsCount));
}
const reversedCols = [];
for (const [index, item] of cols.entries()) {
if (index % 2 === 1) {
reversedCols.push(item.toReversed());
} else {
reversedCols.push(item);
}
}
const result = [];
for (let i = 0; i < rowsCount; i++) {
const row = [];
for (const col of reversedCols) {
row.push(col[i]);
}
result.push(row);
}
return result;
}
/*
export function snail(rowsCount: number, colsCount: number): number[][] {
if (this.length !== rowsCount * colsCount) {
return [];
}
const cols = [];
for (let i = 0; i < colsCount; i++) {
cols.push(this.slice(rowsCount * i, rowsCount * i + rowsCount));
}
const reversedCols = [];
for (const [index, item] of cols.entries()) {
if (index % 2 === 1) {
reversedCols.push(item.toReversed());
} else {
reversedCols.push(item);
}
}
const result = [];
for (let i = 0; i < rowsCount; i++) {
const row = [];
for (const col of reversedCols) {
row.push(col[i]);
}
result.push(row);
}
return result;
}
*/