Skip to content

Commit 86c6b8d

Browse files
committed
feat: add solutions to lc problem: No.0054
No.0054.Spiral Matrix
1 parent d4bc818 commit 86c6b8d

File tree

9 files changed

+929
-431
lines changed

9 files changed

+929
-431
lines changed

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

+418-156
Large diffs are not rendered by default.

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

+424-150
Large diffs are not rendered by default.

solution/0000-0099/0054.Spiral Matrix/Solution.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ class Solution {
22
public:
33
vector<int> spiralOrder(vector<vector<int>>& matrix) {
44
int m = matrix.size(), n = matrix[0].size();
5-
int top = 0, bottom = m - 1, left = 0, right = n - 1;
5+
int dirs[5] = {0, 1, 0, -1, 0};
6+
int i = 0, j = 0, k = 0;
67
vector<int> ans;
7-
while (top <= bottom && left <= right) {
8-
for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]);
9-
for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]);
10-
if (left < right && top < bottom) {
11-
for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]);
12-
for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]);
8+
bool vis[m][n];
9+
memset(vis, false, sizeof(vis));
10+
for (int h = m * n; h; --h) {
11+
ans.push_back(matrix[i][j]);
12+
vis[i][j] = true;
13+
int x = i + dirs[k], y = j + dirs[k + 1];
14+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x][y]) {
15+
k = (k + 1) % 4;
1316
}
14-
++top;
15-
--bottom;
16-
++left;
17-
--right;
17+
i += dirs[k];
18+
j += dirs[k + 1];
1819
}
1920
return ans;
2021
}

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

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
public class Solution {
22
public IList<int> SpiralOrder(int[][] matrix) {
3-
int m = matrix.Length;
4-
int n = matrix[0].Length;
5-
int top = 0, bottom = m - 1, left = 0, right = n - 1;
6-
var ans = new List<int>(m * n);
7-
while (top <= bottom && left <= right)
8-
{
9-
for (int j = left; j <= right; ++j)
10-
{
11-
ans.Add(matrix[top][j]);
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+
bool[,] visited = new bool[m, n];
7+
for (int h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
8+
ans.Add(matrix[i][j]);
9+
visited[i, j] = true;
10+
int x = i + dirs[k], y = j + dirs[k + 1];
11+
if (x < 0 || x >= m || y < 0 || y >= n || visited[x, y]) {
12+
k = (k + 1) % 4;
1213
}
13-
for (int i = top + 1; i <= bottom; ++i)
14-
{
15-
ans.Add(matrix[i][right]);
16-
}
17-
if (left < right && top < bottom)
18-
{
19-
for (int j = right - 1; j >= left; --j)
20-
{
21-
ans.Add(matrix[bottom][j]);
22-
}
23-
for (int i = bottom - 1; i > top; --i)
24-
{
25-
ans.Add(matrix[i][left]);
26-
}
27-
}
28-
++top;
29-
--bottom;
30-
++left;
31-
--right;
14+
i += dirs[k];
15+
j += dirs[k + 1];
3216
}
3317
return ans;
3418
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
func spiralOrder(matrix [][]int) []int {
1+
func spiralOrder(matrix [][]int) (ans []int) {
22
m, n := len(matrix), len(matrix[0])
3-
ans := make([]int, 0, m*n)
4-
5-
top, bottom, left, right := 0, m-1, 0, n-1
6-
for left <= right && top <= bottom {
7-
for i := left; i <= right; i++ {
8-
ans = append(ans, matrix[top][i])
9-
}
10-
for i := top + 1; i <= bottom; i++ {
11-
ans = append(ans, matrix[i][right])
12-
}
13-
if left < right && top < bottom {
14-
for i := right - 1; i >= left; i-- {
15-
ans = append(ans, matrix[bottom][i])
16-
}
17-
for i := bottom - 1; i > top; i-- {
18-
ans = append(ans, matrix[i][left])
19-
}
3+
vis := make([][]bool, m)
4+
for i := range vis {
5+
vis[i] = make([]bool, n)
6+
}
7+
dirs := [5]int{0, 1, 0, -1, 0}
8+
i, j, k := 0, 0, 0
9+
for h := m * n; h > 0; h-- {
10+
ans = append(ans, matrix[i][j])
11+
vis[i][j] = true
12+
x, y := i+dirs[k], j+dirs[k+1]
13+
if x < 0 || x >= m || y < 0 || y >= n || vis[x][y] {
14+
k = (k + 1) % 4
2015
}
21-
top++
22-
bottom--
23-
left++
24-
right--
16+
i, j = i+dirs[k], j+dirs[k+1]
2517
}
26-
27-
return ans
28-
}
18+
return
19+
}

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

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
class Solution {
22
public List<Integer> spiralOrder(int[][] matrix) {
33
int m = matrix.length, n = matrix[0].length;
4-
int top = 0, bottom = m - 1, left = 0, right = n - 1;
4+
int[] dirs = {0, 1, 0, -1, 0};
5+
int i = 0, j = 0, k = 0;
56
List<Integer> ans = new ArrayList<>();
6-
while (left <= right && top <= bottom) {
7-
for (int j = left; j <= right; ++j) {
8-
ans.add(matrix[top][j]);
7+
boolean[][] vis = new boolean[m][n];
8+
for (int h = m * n; h > 0; --h) {
9+
ans.add(matrix[i][j]);
10+
vis[i][j] = true;
11+
int x = i + dirs[k], y = j + dirs[k + 1];
12+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x][y]) {
13+
k = (k + 1) % 4;
914
}
10-
for (int i = top + 1; i <= bottom; ++i) {
11-
ans.add(matrix[i][right]);
12-
}
13-
if (left < right && top < bottom) {
14-
for (int j = right - 1; j >= left; --j) {
15-
ans.add(matrix[bottom][j]);
16-
}
17-
for (int i = bottom - 1; i > top; --i) {
18-
ans.add(matrix[i][left]);
19-
}
20-
}
21-
++top;
22-
--bottom;
23-
++left;
24-
--right;
15+
i += dirs[k];
16+
j += dirs[k + 1];
2517
}
2618
return ans;
2719
}

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

+12-17
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@
55
var spiralOrder = function (matrix) {
66
const m = matrix.length;
77
const n = matrix[0].length;
8-
let [top, bottom, left, right] = [0, m - 1, 0, n - 1];
9-
let ans = [];
10-
while (top <= bottom && left <= right) {
11-
for (let j = left; j <= right; ++j) {
12-
ans.push(matrix[top][j]);
8+
const ans = [];
9+
const vis = new Array(m).fill(0).map(() => new Array(n).fill(false));
10+
const dirs = [0, 1, 0, -1, 0];
11+
for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
12+
ans.push(matrix[i][j]);
13+
vis[i][j] = true;
14+
const x = i + dirs[k];
15+
const y = j + dirs[k + 1];
16+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x][y]) {
17+
k = (k + 1) % 4;
1318
}
14-
for (let i = top + 1; i <= bottom; ++i) {
15-
ans.push(matrix[i][right]);
16-
}
17-
if (left < right && top < bottom) {
18-
for (let j = right - 1; j >= left; --j) {
19-
ans.push(matrix[bottom][j]);
20-
}
21-
for (let i = bottom - 1; i > top; --i) {
22-
ans.push(matrix[i][left]);
23-
}
24-
}
25-
[top, bottom, left, right] = [top + 1, bottom - 1, left + 1, right - 1];
19+
i += dirs[k];
20+
j += dirs[k + 1];
2621
}
2722
return ans;
2823
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
class Solution:
22
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
33
m, n = len(matrix), len(matrix[0])
4+
dirs = (0, 1, 0, -1, 0)
5+
i = j = k = 0
46
ans = []
5-
top, bottom, left, right = 0, m - 1, 0, n - 1
6-
while left <= right and top <= bottom:
7-
ans.extend([matrix[top][j] for j in range(left, right + 1)])
8-
ans.extend([matrix[i][right] for i in range(top + 1, bottom + 1)])
9-
if left < right and top < bottom:
10-
ans.extend([matrix[bottom][j] for j in range(right - 1, left - 1, -1)])
11-
ans.extend([matrix[i][left] for i in range(bottom - 1, top, -1)])
12-
top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1
7+
vis = set()
8+
for _ in range(m * n):
9+
ans.append(matrix[i][j])
10+
vis.add((i, j))
11+
x, y = i + dirs[k], j + dirs[k + 1]
12+
if not 0 <= x < m or not 0 <= y < n or (x, y) in vis:
13+
k = (k + 1) % 4
14+
i = i + dirs[k]
15+
j = j + dirs[k + 1]
1316
return ans
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
function spiralOrder(matrix: number[][]): number[] {
22
const m = matrix.length;
33
const n = matrix[0].length;
4-
const res = [];
5-
for (let i = 0; i <= m >> 1; i++) {
6-
for (let j = i; j < n - i - 1; j++) {
7-
res.push(matrix[i][j]);
4+
const ans: number[] = [];
5+
const vis = new Array(m).fill(0).map(() => new Array(n).fill(false));
6+
const dirs = [0, 1, 0, -1, 0];
7+
for (let h = m * n, i = 0, j = 0, k = 0; h > 0; --h) {
8+
ans.push(matrix[i][j]);
9+
vis[i][j] = true;
10+
const x = i + dirs[k];
11+
const y = j + dirs[k + 1];
12+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x][y]) {
13+
k = (k + 1) % 4;
814
}
9-
for (let j = i; j < m - i - 1; j++) {
10-
res.push(matrix[j][n - i - 1]);
11-
}
12-
for (let j = i; j < n - i - 1; j++) {
13-
res.push(matrix[m - i - 1][n - j - 1]);
14-
}
15-
for (let j = i; j < m - i - 1; j++) {
16-
res.push(matrix[m - j - 1][i]);
17-
}
18-
}
19-
if (m & 1) {
20-
res.push(matrix[m >> 1][n >> 1]);
15+
i += dirs[k];
16+
j += dirs[k + 1];
2117
}
22-
return res.slice(0, m * n);
18+
return ans;
2319
}

0 commit comments

Comments
 (0)