Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0059 (#4037)
Browse files Browse the repository at this point in the history
No.0059.Spiral Matrix II
  • Loading branch information
yanglbme authored Feb 7, 2025
1 parent 3ca7f17 commit 2bfa7a9
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 335 deletions.
169 changes: 52 additions & 117 deletions solution/0000-0099/0059.Spiral Matrix II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ tags:

### 方法一:模拟

直接模拟螺旋矩阵的生成过程
我们可以直接模拟螺旋矩阵的生成过程

定义一个二维数组 `ans`,用于存储螺旋矩阵。用 `i``j` 分别表示当前位置的行号和列号,用 `k` 表示当前的方向编号,`dirs` 表示方向编号与方向的对应关系。
定义一个二维数组 $\textit{ans}$,用于存储螺旋矩阵。用 $i$$j$ 分别表示当前位置的行号和列号,用 $k$ 表示当前的方向编号,$\textit{dirs}$ 表示方向编号与方向的对应关系。

`1` 开始,依次填入矩阵中的每个位置。每次填入一个位置后,计算下一个位置的行号和列号,如果下一个位置不在矩阵中或者已经被填过,则改变方向,再计算下一个位置的行号和列号。
$1$ 开始,依次填入矩阵中的每个位置。每次填入一个位置后,计算下一个位置的行号和列号,如果下一个位置不在矩阵中或者已经被填过,则改变方向,再计算下一个位置的行号和列号。

时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的边长。忽略输出数组不计,空间复杂度 $O(1)$。
时间复杂度 $O(n^2)$,其中 $n$ 是矩阵的边长。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -68,15 +68,14 @@ tags:
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
ans = [[0] * n for _ in range(n)]
dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
dirs = (0, 1, 0, -1, 0)
i = j = k = 0
for v in range(1, n * n + 1):
ans[i][j] = v
x, y = i + dirs[k][0], j + dirs[k][1]
if x < 0 or y < 0 or x >= n or y >= n or ans[x][y]:
x, y = i + dirs[k], j + dirs[k + 1]
if x < 0 or x >= n or y < 0 or y >= n or ans[x][y]:
k = (k + 1) % 4
x, y = i + dirs[k][0], j + dirs[k][1]
i, j = x, y
i, j = i + dirs[k], j + dirs[k + 1]
return ans
```

Expand All @@ -86,18 +85,16 @@ class Solution:
class Solution {
public int[][] generateMatrix(int n) {
int[][] ans = new int[n][n];
final int[] dirs = {0, 1, 0, -1, 0};
int i = 0, j = 0, k = 0;
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
for (int v = 1; v <= n * n; ++v) {
ans[i][j] = v;
int x = i + dirs[k][0], y = j + dirs[k][1];
if (x < 0 || y < 0 || x >= n || y >= n || ans[x][y] > 0) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x < 0 || x >= n || y < 0 || y >= n || ans[x][y] != 0) {
k = (k + 1) % 4;
x = i + dirs[k][0];
y = j + dirs[k][1];
}
i = x;
j = y;
i += dirs[k];
j += dirs[k + 1];
}
return ans;
}
Expand All @@ -109,19 +106,18 @@ class Solution {
```cpp
class Solution {
public:
const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n));
vector<vector<int>> ans(n, vector<int>(n, 0));
const int dirs[5] = {0, 1, 0, -1, 0};
int i = 0, j = 0, k = 0;
for (int v = 1; v <= n * n; ++v) {
ans[i][j] = v;
int x = i + dirs[k][0], y = j + dirs[k][1];
if (x < 0 || y < 0 || x >= n || y >= n || ans[x][y]) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x < 0 || x >= n || y < 0 || y >= n || ans[x][y] != 0) {
k = (k + 1) % 4;
x = i + dirs[k][0], y = j + dirs[k][1];
}
i = x, j = y;
i += dirs[k];
j += dirs[k + 1];
}
return ans;
}
Expand All @@ -136,16 +132,16 @@ func generateMatrix(n int) [][]int {
for i := range ans {
ans[i] = make([]int, n)
}
dirs := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
var i, j, k int
dirs := [5]int{0, 1, 0, -1, 0}
i, j, k := 0, 0, 0
for v := 1; v <= n*n; v++ {
ans[i][j] = v
x, y := i+dirs[k][0], j+dirs[k][1]
if x < 0 || y < 0 || x >= n || y >= n || ans[x][y] > 0 {
x, y := i+dirs[k], j+dirs[k+1]
if x < 0 || x >= n || y < 0 || y >= n || ans[x][y] != 0 {
k = (k + 1) % 4
x, y = i+dirs[k][0], j+dirs[k][1]
}
i, j = x, y
i += dirs[k]
j += dirs[k+1]
}
return ans
}
Expand All @@ -155,24 +151,17 @@ func generateMatrix(n int) [][]int {

```ts
function generateMatrix(n: number): number[][] {
let ans = Array.from({ length: n }, v => new Array(n));
let dir = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let i = 0,
j = 0;
for (let cnt = 1, k = 0; cnt <= n * n; cnt++) {
ans[i][j] = cnt;
let x = i + dir[k][0],
y = j + dir[k][1];
if (x < 0 || x == n || y < 0 || y == n || ans[x][y]) {
const ans: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
const dirs = [0, 1, 0, -1, 0];
let [i, j, k] = [0, 0, 0];
for (let v = 1; v <= n * n; v++) {
ans[i][j] = v;
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x < 0 || x >= n || y < 0 || y >= n || ans[x][y] !== 0) {
k = (k + 1) % 4;
(x = i + dir[k][0]), (y = j + dir[k][1]);
}
(i = x), (j = y);
i += dirs[k];
j += dirs[k + 1];
}
return ans;
}
Expand All @@ -183,31 +172,19 @@ function generateMatrix(n: number): number[][] {
```rust
impl Solution {
pub fn generate_matrix(n: i32) -> Vec<Vec<i32>> {
let n = n as usize;
let mut res = vec![vec![0; n]; n];
let mut num = 1;
for i in 0..n / 2 {
for j in i..n - i - 1 {
res[i][j] = num;
num += 1;
}
for j in i..n - i - 1 {
res[j][n - i - 1] = num;
num += 1;
}
for j in i..n - i - 1 {
res[n - i - 1][n - j - 1] = num;
num += 1;
}
for j in i..n - i - 1 {
res[n - j - 1][i] = num;
num += 1;
let mut ans = vec![vec![0; n as usize]; n as usize];
let dirs = [0, 1, 0, -1, 0];
let (mut i, mut j, mut k) = (0, 0, 0);
for v in 1..=n * n {
ans[i as usize][j as usize] = v;
let (x, y) = (i + dirs[k], j + dirs[k + 1]);
if x < 0 || x >= n || y < 0 || y >= n || ans[x as usize][y as usize] != 0 {
k = (k + 1) % 4;
}
i += dirs[k];
j += dirs[k + 1];
}
if n % 2 == 1 {
res[n >> 1][n >> 1] = num;
}
res
ans
}
}
```
Expand All @@ -220,22 +197,17 @@ impl Solution {
* @return {number[][]}
*/
var generateMatrix = function (n) {
const ans = new Array(n).fill(0).map(() => new Array(n).fill(0));
const ans = Array.from({ length: n }, () => Array(n).fill(0));
const dirs = [0, 1, 0, -1, 0];
let [i, j, k] = [0, 0, 0];
const dirs = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
for (let v = 1; v <= n * n; ++v) {
for (let v = 1; v <= n * n; v++) {
ans[i][j] = v;
let [x, y] = [i + dirs[k][0], j + dirs[k][1]];
if (x < 0 || y < 0 || x >= n || y >= n || ans[x][y] > 0) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x < 0 || x >= n || y < 0 || y >= n || ans[x][y] !== 0) {
k = (k + 1) % 4;
[x, y] = [i + dirs[k][0], j + dirs[k][1]];
}
[i, j] = [x, y];
i += dirs[k];
j += dirs[k + 1];
}
return ans;
};
Expand All @@ -245,41 +217,4 @@ var generateMatrix = function (n) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### TypeScript

```ts
function generateMatrix(n: number): number[][] {
const res = new Array(n).fill(0).map(() => new Array(n).fill(0));
let num = 1;
for (let i = 0; i < Math.floor(n / 2); i++) {
for (let j = i; j < n - i - 1; j++) {
res[i][j] = num++;
}
for (let j = i; j < n - i - 1; j++) {
res[j][n - i - 1] = num++;
}
for (let j = i; j < n - i - 1; j++) {
res[n - i - 1][n - j - 1] = num++;
}
for (let j = i; j < n - i - 1; j++) {
res[n - j - 1][i] = num++;
}
}
if (n % 2 === 1) {
res[n >> 1][n >> 1] = num;
}
return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading

0 comments on commit 2bfa7a9

Please sign in to comment.