59.螺旋矩阵 II:优质代码无多层嵌套,Python主程序11行解决

【LetMeFly】59.螺旋矩阵 II:优质代码无多层嵌套,Python主程序11行解决

力扣题目链接:https://leetcode.cn/problems/spiral-matrix-ii/

给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix

 

示例 1:

输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]

示例 2:

输入:n = 1
输出:[[1]]

 

提示:

  • 1 <= n <= 20

解题方法:模拟(定义方向数组)

定义一个方向数组directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}},代表当前前进方向下,下一个元素的下标相对这一个元素的下标的横纵坐标diff

四个方向分别代表👉👇👈👆,初始方向为👉(令d代表方向,初始值为0)。

前进过程中,如果前方元素是边界,或者前方元素已经被填充过,则换向(d = (d + 1) % 4)。

  • 时间复杂度$O(n^2)$
  • 空间复杂度$O(1)$,返回值不计入

AC代码

C++

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
/*
* @Author: LetMeFly
* @Date: 2025-02-07 20:37:54
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-02-07 20:42:22
*/

class Solution {
private:
const int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n));
int d = 0, x = 0, y = 0;
for (int i = 1; i <= n * n; i++) {
ans[x][y] = i;
int nx = x + directions[d][0];
int ny = y + directions[d][1];
if (nx < 0 || nx == n || ny < 0 || ny == n || ans[nx][ny]) {
d = (d + 1) % 4;
nx = x + directions[d][0];
ny = y + directions[d][1];
}
x = nx, y = ny;
}
return ans;
}
};

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
Author: LetMeFly
Date: 2025-02-07 20:38:00
LastEditors: LetMeFly.xyz
LastEditTime: 2025-02-07 20:49:54
'''
from typing import List


class Solution:
def __init__(self):
self.directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]

def generateMatrix(self, n: int) -> List[List[int]]:
ans = [[0] * n for _ in range(n)]
d = x = y = 0
for i in range(n * n):
ans[x][y] = i + 1
nx, ny = x + self.directions[d][0], y + self.directions[d][1]
if nx == n or nx < 0 or ny == n or ny < 0 or ans[nx][ny]:
d = (d + 1) % 4
nx, ny = x + self.directions[d][0], y + self.directions[d][1]
x, y = nx, ny
return ans

Java

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
/*
* @Author: LetMeFly
* @Date: 2025-02-07 20:38:06
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-02-07 20:52:38
*/
class Solution {
private int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

public int[][] generateMatrix(int n) {
int[][] ans = new int[n][n];
int d = 0, x = 0, y = 0;
for (int i = 1; i <= n * n; i++) {
ans[x][y] = i;
int nx = x + directions[d][0];
int ny = y + directions[d][1];
if (nx == n || nx < 0 || ny == n || ny < 0 || ans[nx][ny] > 0) {
d = (d + 1) % 4;
nx = x + directions[d][0];
ny = y + directions[d][1];
}
x = nx;
y = ny;
}
return ans;
}
}

Go

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
/*
* @Author: LetMeFly
* @Date: 2025-02-07 20:38:08
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2025-02-07 20:57:00
*/
package main

var directions [][]int = [][]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
}

func generateMatrix(n int) [][]int {
ans := make([][]int, n)
for i := range ans {
ans[i] = make([]int, n)
}
d, x, y := 0, 0, 0
for i := 1; i <= n * n; i++ {
ans[x][y] = i
nx, ny := x + directions[d][0], y + directions[d][1]
if nx == n || nx < 0 || ny == n || ny < 0 || ans[nx][ny] > 0 {
d = (d + 1) % 4
nx, ny = x + directions[d][0], y + directions[d][1]
}
x, y = nx, ny
}
return ans
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

Tisfy:https://letmefly.blog.csdn.net/article/details/145502589


59.螺旋矩阵 II:优质代码无多层嵌套,Python主程序11行解决
https://blog.letmefly.xyz/2025/02/07/LeetCode 0059.螺旋矩阵II/
作者
Tisfy
发布于
2025年2月7日
许可协议