Skip to content

Commit 34073f2

Browse files
committed
feat: add solutions to lc problem: No.0054
No.0054.Spiral Matrix
1 parent 5b8e26b commit 34073f2

File tree

3 files changed

+277
-89
lines changed

3 files changed

+277
-89
lines changed

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

Lines changed: 143 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:逐层模拟**
43+
4244
从外往里一圈一圈遍历并存储矩阵元素即可。
4345

46+
时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
47+
48+
**方法二:模拟**
49+
50+
我们用 $i$ 和 $j$ 分别表示当前访问到的元素的行和列,用 $k$ 表示当前的方向,用数组或哈希表 $vis$ 记录每个元素是否被访问过。每次我们访问到一个元素后,将其标记为已访问,然后按照当前的方向前进一步,如果前进一步后发现越界或者已经访问过,则改变方向继续前进,直到遍历完整个矩阵。
51+
52+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
53+
4454
<!-- tabs:start -->
4555

4656
### **Python3**
@@ -63,6 +73,25 @@ class Solution:
6373
return ans
6474
```
6575

76+
```python
77+
class Solution:
78+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
79+
m, n = len(matrix), len(matrix[0])
80+
dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
81+
i = j = k = 0
82+
ans = []
83+
vis = [[False] * n for _ in range(m)]
84+
for _ in range(m * n):
85+
ans.append(matrix[i][j])
86+
vis[i][j] = True
87+
x, y = i + dirs[k][0], j + dirs[k][1]
88+
if x < 0 or y < 0 or x >= m or y >= n or vis[x][y]:
89+
k = (k + 1) % 4
90+
x, y = i + dirs[k][0], j + dirs[k][1]
91+
i, j = x, y
92+
return ans
93+
```
94+
6695
### **Java**
6796

6897
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -98,36 +127,79 @@ class Solution {
98127
}
99128
```
100129

101-
### **JavaScript**
102-
103-
```js
104-
/**
105-
* @param {number[][]} matrix
106-
* @return {number[]}
107-
*/
108-
var spiralOrder = function (matrix) {
109-
const m = matrix.length;
110-
const n = matrix[0].length;
111-
let [top, bottom, left, right] = [0, m - 1, 0, n - 1];
112-
let ans = [];
113-
while (top <= bottom && left <= right) {
114-
for (let j = left; j <= right; ++j) {
115-
ans.push(matrix[top][j]);
116-
}
117-
for (let i = top + 1; i <= bottom; ++i) {
118-
ans.push(matrix[i][right]);
130+
```java
131+
class Solution {
132+
public List<Integer> spiralOrder(int[][] matrix) {
133+
int m = matrix.length, n = matrix[0].length;
134+
int i = 0, j = 0, k = 0;
135+
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
136+
List<Integer> ans = new ArrayList<>();
137+
boolean[][] vis = new boolean[m][n];
138+
for (int h = 0; h < m * n; ++h) {
139+
ans.add(matrix[i][j]);
140+
vis[i][j] = true;
141+
int x = i + dirs[k][0], y = j + dirs[k][1];
142+
if (x < 0 || y < 0 || x >= m || y >= n || vis[x][y]) {
143+
k = (k + 1) % 4;
144+
x = i + dirs[k][0];
145+
y = j + dirs[k][1];
146+
}
147+
i = x;
148+
j = y;
119149
}
120-
if (left < right && top < bottom) {
121-
for (let j = right - 1; j >= left; --j) {
122-
ans.push(matrix[bottom][j]);
150+
return ans;
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
161+
int m = matrix.size(), n = matrix[0].size();
162+
int top = 0, bottom = m - 1, left = 0, right = n - 1;
163+
vector<int> ans;
164+
while (top <= bottom && left <= right) {
165+
for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]);
166+
for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]);
167+
if (left < right && top < bottom) {
168+
for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]);
169+
for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]);
123170
}
124-
for (let i = bottom - 1; i > top; --i) {
125-
ans.push(matrix[i][left]);
171+
++top;
172+
--bottom;
173+
++left;
174+
--right;
175+
}
176+
return ans;
177+
}
178+
};
179+
```
180+
181+
```cpp
182+
class Solution {
183+
public:
184+
const int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
185+
186+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
187+
int m = matrix.size(), n = matrix[0].size();
188+
int i = 0, j = 0, k = 0;
189+
vector<int> ans;
190+
bool vis[11][11] = {0};
191+
for (int h = 0; h < m * n; ++h) {
192+
ans.push_back(matrix[i][j]);
193+
vis[i][j] = 1;
194+
int x = i + dirs[k][0], y = j + dirs[k][1];
195+
if (x < 0 || x >= m || y < 0 || y >= n || vis[x][y]) {
196+
k = (k + 1) % 4;
197+
x = i + dirs[k][0], y = j + dirs[k][1];
126198
}
199+
i = x, j = y;
127200
}
128-
[top, bottom, left, right] = [top + 1, bottom - 1, left + 1, right - 1];
201+
return ans;
129202
}
130-
return ans;
131203
};
132204
```
133205

@@ -164,29 +236,56 @@ func spiralOrder(matrix [][]int) []int {
164236
}
165237
```
166238

167-
### **C++**
239+
```go
240+
func spiralOrder(matrix [][]int) (ans []int) {
241+
m, n := len(matrix), len(matrix[0])
242+
var i, j, k int
243+
dirs := [4][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
244+
vis := [11][11]bool{}
245+
for h := 0; h < m*n; h++ {
246+
ans = append(ans, matrix[i][j])
247+
vis[i][j] = true
248+
x, y := i+dirs[k][0], j+dirs[k][1]
249+
if x < 0 || x >= m || y < 0 || y >= n || vis[x][y] {
250+
k = (k + 1) % 4
251+
x, y = i+dirs[k][0], j+dirs[k][1]
252+
}
253+
i, j = x, y
254+
}
255+
return
256+
}
257+
```
168258

169-
```cpp
170-
class Solution {
171-
public:
172-
vector<int> spiralOrder(vector<vector<int>>& matrix) {
173-
int m = matrix.size(), n = matrix[0].size();
174-
int top = 0, bottom = m - 1, left = 0, right = n - 1;
175-
vector<int> ans;
176-
while (top <= bottom && left <= right) {
177-
for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]);
178-
for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]);
179-
if (left < right && top < bottom) {
180-
for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]);
181-
for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]);
259+
### **JavaScript**
260+
261+
```js
262+
/**
263+
* @param {number[][]} matrix
264+
* @return {number[]}
265+
*/
266+
var spiralOrder = function (matrix) {
267+
const m = matrix.length;
268+
const n = matrix[0].length;
269+
let [top, bottom, left, right] = [0, m - 1, 0, n - 1];
270+
let ans = [];
271+
while (top <= bottom && left <= right) {
272+
for (let j = left; j <= right; ++j) {
273+
ans.push(matrix[top][j]);
274+
}
275+
for (let i = top + 1; i <= bottom; ++i) {
276+
ans.push(matrix[i][right]);
277+
}
278+
if (left < right && top < bottom) {
279+
for (let j = right - 1; j >= left; --j) {
280+
ans.push(matrix[bottom][j]);
281+
}
282+
for (let i = bottom - 1; i > top; --i) {
283+
ans.push(matrix[i][left]);
182284
}
183-
++top;
184-
--bottom;
185-
++left;
186-
--right;
187285
}
188-
return ans;
286+
[top, bottom, left, right] = [top + 1, bottom - 1, left + 1, right - 1];
189287
}
288+
return ans;
190289
};
191290
```
192291

0 commit comments

Comments
 (0)