Skip to content

Commit de64b87

Browse files
committed
https://leetcode.cn/problems/snail-traversal/
1 parent 449ca87 commit de64b87

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode.cn/problems/snail-traversal/
53+
5254
https://leetcode.cn/problems/function-composition
5355

5456
https://leetcode.cn/problems/array-reduce-transformation/

snail-traversal/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
declare global {
2+
interface Array<T> {
3+
snail(rowsCount: number, colsCount: number): number[][];
4+
}
5+
}
6+
7+
Array.prototype.snail = function (
8+
rowsCount: number,
9+
colsCount: number,
10+
): number[][] {
11+
// deno-lint-ignore no-this-alias
12+
const nums = this;
13+
if (
14+
rowsCount * colsCount !== this.length
15+
) return [];
16+
return Array.from(
17+
{ length: rowsCount },
18+
(_, i) =>
19+
Array.from(
20+
{ length: colsCount },
21+
(_, j) =>
22+
j & 1
23+
? nums[rowsCount - 1 - i + rowsCount * j]
24+
: nums[i + rowsCount * j],
25+
),
26+
);
27+
};

0 commit comments

Comments
 (0)