Skip to content

Commit 26e2028

Browse files
committed
[新增] 新增题目 59. 螺旋矩阵 II
1 parent eff8227 commit 26e2028

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/59_spiralMatrixIi.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
59. 螺旋矩阵II
3+
难度:中等
4+
题目链接:https://leetcode.cn/problems/spiral-matrix-ii/
5+
解析:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html#_59-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5ii
6+
7+
给你一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix。
8+
9+
示例 1:
10+
输入:n = 3
11+
输出:[[1,2,3],[8,9,4],[7,6,5]]
12+
13+
示例 2:
14+
输入:n = 1
15+
输出:[[1]]
16+
17+
提示:
18+
1 <= n <= 20
19+
*/
20+
21+
import { fileURLToPath } from 'url'
22+
import testCases from '../tests/testCases/59_spiralMatrixIi.js'
23+
24+
/**
25+
* 螺旋矩阵II
26+
* - LeetCode 入口
27+
* @param {number} n - 正整数 N
28+
* @returns {string} 螺旋矩阵
29+
*/
30+
export function generateMatrix (n: number): number[][] {
31+
// 初始化长宽为 N 的矩阵,所有值使用 0 填充
32+
const result: number[][] = new Array(n).fill(0).map(
33+
() => new Array(n).fill(0)
34+
)
35+
36+
// 初始化坐标轴
37+
let x = 0; let y = 0
38+
39+
// 初始化每一条边所走的步数
40+
// 每一次走边长减一的步数,然后顺时针转弯走下一条边
41+
// 上一条边的最后一个点作为下一条边的起点,每一条边按照左闭右开绘制
42+
// 顺时针按照上、右、下、左走完四条边后开始向内开启下一圈,每圈步数 - 2
43+
let steps = n - 1
44+
45+
// 初始化填充值
46+
let value = 1
47+
48+
// 填充螺旋矩阵直到步长为 0
49+
while (steps > 0) {
50+
// 填充上边
51+
for (let step = 0; step < steps; step++) result[x][y++] = value++
52+
53+
// 填充右边
54+
for (let step = 0; step < steps; step++) result[x++][y] = value++
55+
56+
// 填充下边
57+
for (let step = 0; step < steps; step++) result[x][y--] = value++
58+
59+
// 填充左边
60+
for (let step = 0; step < steps; step++) result[x--][y] = value++
61+
62+
// 一圈完成,边长 - 2
63+
steps -= 2
64+
65+
// 将坐标设置向内一圈的起点
66+
y++; x++
67+
}
68+
69+
// N 为奇数时需要手动填充矩阵最中心的值
70+
if ((n & 1) === 1) {
71+
const middleXY = Math.floor(n / 2)
72+
result[middleXY][middleXY] = value
73+
}
74+
75+
return result
76+
}
77+
78+
// Debug
79+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
80+
const { input, expected } = testCases[0]
81+
const output = generateMatrix(input)
82+
console.log({ input, output, expected })
83+
}

tests/59_spiralMatrixIi.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, test, expect } from '@jest/globals'
2+
import testCases from './testCases/59_spiralMatrixIi.js'
3+
import { generateMatrix } from '../src/59_spiralMatrixIi.js'
4+
5+
describe('59. Spiral Matrix II', () => {
6+
testCases.forEach(({ input, expected }, index) => {
7+
test(`Test case index: ${index}, Input: ${input}`, () => {
8+
expect(generateMatrix(input)).toEqual(expected)
9+
})
10+
})
11+
})

tests/testCases/59_spiralMatrixIi.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default [
2+
{ input: 3, expected: [[1, 2, 3], [8, 9, 4], [7, 6, 5]] },
3+
{ input: 1, expected: [[1]] }
4+
]

0 commit comments

Comments
 (0)