Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 988caec

Browse files
feat: add solutions to lc problem: No.0054 (doocs#3897)
No.0054.Spiral Matrix
1 parent 1a6ab2a commit 988caec

33 files changed

+621
-1123
lines changed

‎solution/0000-0099/0054.Spiral Matrix/README.md‎

Lines changed: 125 additions & 319 deletions
Large diffs are not rendered by default.

‎solution/0000-0099/0054.Spiral Matrix/README_EN.md‎

Lines changed: 125 additions & 319 deletions
Large diffs are not rendered by default.

‎solution/0000-0099/0054.Spiral Matrix/Solution.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
public class Solution {
22
public IList<int> SpiralOrder(int[][] matrix) {
33
int m = matrix.Length, n = matrix[0].Length;
4-
int[] dirs = new int[] {0, 1, 0, -1, 0};
4+
int[] dirs = { 0, 1, 0, -1, 0 };
5+
int i = 0, j = 0, k = 0;
56
IList<int> ans = new List<int>();
6-
bool[,] visited = new bool[m, n];
7-
for (int h = m * n,i=0,j=0,k=0; h > 0; --h) {
7+
bool[,] vis = new bool[m, n];
8+
for (int h = m * n; h > 0; --h) {
89
ans.Add(matrix[i][j]);
9-
visited[i, j] = true;
10+
vis[i, j] = true;
1011
int x = i + dirs[k], y = j + dirs[k + 1];
11-
if (x < 0 || x >= m || y < 0 || y >= n || visited[x, y]) {
12+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x, y]) {
1213
k = (k + 1) % 4;
1314
}
1415
i += dirs[k];

‎solution/0000-0099/0054.Spiral Matrix/Solution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public List<Integer> spiralOrder(int[][] matrix) {
1717
}
1818
return ans;
1919
}
20-
}
20+
}

‎solution/0000-0099/0054.Spiral Matrix/Solution.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var spiralOrder = function (matrix) {
66
const m = matrix.length;
77
const n = matrix[0].length;
88
const ans = [];
9-
const vis = newArray(m).fill(0).map(() =>new Array(n).fill(false));
9+
const vis = Array.from({length: m},() => Array(n).fill(false));
1010
const dirs = [0, 1, 0, -1, 0];
1111
for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
1212
ans.push(matrix[i][j]);

‎solution/0000-0099/0054.Spiral Matrix/Solution.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ class Solution:
22
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
33
m, n = len(matrix), len(matrix[0])
44
dirs = (0, 1, 0, -1, 0)
5+
vis = [[False] * n for _ in range(m)]
56
i = j = k = 0
67
ans = []
7-
vis = set()
88
for _ in range(m * n):
99
ans.append(matrix[i][j])
10-
vis.add((i, j))
10+
vis[i][j] =True
1111
x, y = i + dirs[k], j + dirs[k + 1]
12-
if not0<= x < m or not0<= y < n or (x, y) invis:
12+
if x<0or x >= m or y<0or y >= n or vis[x][y]:
1313
k = (k + 1) % 4
14-
i =i+ dirs[k]
15-
j =j+ dirs[k + 1]
14+
i += dirs[k]
15+
j += dirs[k + 1]
1616
return ans
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
impl Solution {
22
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
3-
let mut x1 = 0;
4-
let mut y1 = 0;
5-
let mut x2 = matrix.len() - 1;
6-
let mut y2 = matrix[0].len() - 1;
7-
let mut result = vec![];
3+
let m = matrix.len();
4+
let n = matrix[0].len();
5+
let mut dirs = vec![0, 1, 0, -1, 0];
6+
let mut vis = vec![vec![false; n]; m];
7+
let mut i = 0;
8+
let mut j = 0;
9+
let mut k = 0;
10+
let mut ans = Vec::new();
811

9-
while x1 <= x2 && y1 <= y2 {
10-
for j in y1..=y2 {
11-
result.push(matrix[x1][j]);
12-
}
13-
for i in x1 + 1..=x2 {
14-
result.push(matrix[i][y2]);
15-
}
16-
if x1 < x2 && y1 < y2 {
17-
for j in (y1..y2).rev() {
18-
result.push(matrix[x2][j]);
19-
}
20-
for i in (x1 + 1..x2).rev() {
21-
result.push(matrix[i][y1]);
22-
}
23-
}
24-
x1 += 1;
25-
y1 += 1;
26-
if x2 != 0 {
27-
x2 -= 1;
28-
}
29-
if y2 != 0 {
30-
y2 -= 1;
12+
for _ in 0..(m * n) {
13+
ans.push(matrix[i][j]);
14+
vis[i][j] = true;
15+
let x = i as i32 + dirs[k] as i32;
16+
let y = j as i32 + dirs[k + 1] as i32;
17+
18+
if x < 0 || x >= m as i32 || y < 0 || y >= n as i32 || vis[x as usize][y as usize] {
19+
k = (k + 1) % 4;
3120
}
21+
22+
i = (i as i32 + dirs[k] as i32) as usize;
23+
j = (j as i32 + dirs[k + 1] as i32) as usize;
3224
}
33-
return result;
25+
26+
ans
3427
}
3528
}

‎solution/0000-0099/0054.Spiral Matrix/Solution.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function spiralOrder(matrix: number[][]): number[] {
22
const m = matrix.length;
33
const n = matrix[0].length;
44
const ans: number[] = [];
5-
const vis=newArray(m).fill(0).map(() =>new Array(n).fill(false));
5+
const vis: boolean[][]=Array.from({length: m},() => Array(n).fill(false));
66
const dirs = [0, 1, 0, -1, 0];
77
for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
88
ans.push(matrix[i][j]);
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
class Solution {
2-
public:
3-
vector<int> spiralOrder(vector<vector<int>>& matrix) {
4-
int m = matrix.size(), n = matrix[0].size();
5-
int dirs[5] = {0, 1, 0, -1, 0};
6-
vector<int> ans;
7-
for (int h = m * n, i = 0, j = 0, k = 0; h; --h) {
8-
ans.push_back(matrix[i][j]);
9-
matrix[i][j] += 300;
10-
int x = i + dirs[k], y = j + dirs[k + 1];
11-
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) {
12-
k = (k + 1) % 4;
13-
}
14-
i += dirs[k];
15-
j += dirs[k + 1];
16-
}
17-
// for (int i = 0; i < m; ++i) {
18-
// for (int j = 0; j < n; ++j) {
19-
// matrix[i][j] -= 300;
20-
// }
21-
// }
22-
return ans;
23-
}
24-
};
1+
class Solution {
2+
public:
3+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
4+
int m = matrix.size(), n = matrix[0].size();
5+
int dirs[5] = {0, 1, 0, -1, 0};
6+
int i = 0, j = 0, k = 0;
7+
vector<int> ans;
8+
for (int h = m * n; h; --h) {
9+
ans.push_back(matrix[i][j]);
10+
matrix[i][j] += 300;
11+
int x = i + dirs[k], y = j + dirs[k + 1];
12+
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) {
13+
k = (k + 1) % 4;
14+
}
15+
i += dirs[k];
16+
j += dirs[k + 1];
17+
}
18+
for (i = 0; i < m; ++i) {
19+
for (j = 0; j < n; ++j) {
20+
matrix[i][j] -= 300;
21+
}
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
public class Solution {
2-
public IList<int> SpiralOrder(int[][] matrix) {
3-
int m = matrix.Length, n = matrix[0].Length;
4-
int[] dirs = new int[] {0, 1, 0, -1, 0};
5-
IList<int> ans = new List<int>();
6-
for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
7-
ans.Add(matrix[i][j]);
8-
matrix[i][j] += 300;
9-
int x = i + dirs[k], y = j + dirs[k + 1];
10-
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) {
11-
k = (k + 1) % 4;
12-
}
13-
i += dirs[k];
14-
j += dirs[k + 1];
15-
}
16-
for (int i = 0; i < m; ++i) {
17-
for (int j = 0; j < n; ++j) {
18-
matrix[i][j] -= 300;
19-
}
20-
}
21-
return ans;
22-
}
23-
}
1+
public class Solution {
2+
public IList<int> SpiralOrder(int[][] matrix) {
3+
int m = matrix.Length, n = matrix[0].Length;
4+
int[] dirs = { 0, 1, 0, -1, 0 };
5+
int i = 0, j = 0, k = 0;
6+
IList<int> ans = new List<int>();
7+
for (int h = m * n; h > 0; --h) {
8+
ans.Add(matrix[i][j]);
9+
matrix[i][j] += 300;
10+
int x = i + dirs[k], y = j + dirs[k + 1];
11+
if (x < 0 || x >= m || y < 0 || y >= n || matrix[x][y] > 100) {
12+
k = (k + 1) % 4;
13+
}
14+
i += dirs[k];
15+
j += dirs[k + 1];
16+
}
17+
for (int a = 0; a < m; ++a) {
18+
for (int b = 0; b < n; ++b) {
19+
matrix[a][b] -= 300;
20+
}
21+
}
22+
return ans;
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /