Skip to content

Commit 6204107

Browse files
committed
feat: update solutions to lc problem: No.0054
No.0054.Spiral Matrix
1 parent 5b1879a commit 6204107

File tree

8 files changed

+265
-449
lines changed

8 files changed

+265
-449
lines changed

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

+88-147
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@
5050
```python
5151
class Solution:
5252
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
53-
def add(i1, j1, i2, j2):
54-
if i1 == i2:
55-
return [matrix[i1][j] for j in range(j1, j2 + 1)]
56-
if j1 == j2:
57-
return [matrix[i][j1] for i in range(i1, i2 + 1)]
58-
return [matrix[i1][j] for j in range(j1, j2)] + [matrix[i][j2] for i in range(i1, i2)] + [matrix[i2][j] for j in range(j2, j1, -1)] + [matrix[i][j1] for i in range(i2, i1, -1)]
5953
m, n = len(matrix), len(matrix[0])
60-
i1, j1, i2, j2 = 0, 0, m - 1, n - 1
61-
res = []
62-
while i1 <= i2 and j1 <= j2:
63-
res += add(i1, j1, i2, j2)
64-
i1, j1, i2, j2 = i1 + 1, j1 + 1, i2 - 1, j2 - 1
65-
return res
54+
ans = []
55+
top, bottom, left, right = 0, m - 1, 0, n - 1
56+
while left <= right and top <= bottom:
57+
ans.extend([matrix[top][j] for j in range(left, right + 1)])
58+
ans.extend([matrix[i][right] for i in range(top + 1, bottom + 1)])
59+
if left < right and top < bottom:
60+
ans.extend([matrix[bottom][j]
61+
for j in range(right - 1, left - 1, -1)])
62+
ans.extend([matrix[i][left]
63+
for i in range(bottom - 1, top, -1)])
64+
top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1
65+
return ans
6666
```
6767

6868
### **Java**
@@ -71,44 +71,31 @@ class Solution:
7171

7272
```java
7373
class Solution {
74-
private List<Integer> res;
75-
7674
public List<Integer> spiralOrder(int[][] matrix) {
7775
int m = matrix.length, n = matrix[0].length;
78-
res = new ArrayList<>();
79-
int i1 = 0, i2 = m - 1;
80-
int j1 = 0, j2 = n - 1;
81-
while (i1 <= i2 && j1 <= j2) {
82-
add(matrix, i1++, j1++, i2--, j2--);
83-
}
84-
return res;
85-
}
86-
87-
private void add(int[][] matrix, int i1, int j1, int i2, int j2) {
88-
if (i1 == i2) {
89-
for (int j = j1; j <= j2; ++j) {
90-
res.add(matrix[i1][j]);
76+
int top = 0, bottom = m - 1, left = 0, right = n - 1;
77+
List<Integer> ans = new ArrayList<>();
78+
while (left <= right && top <= bottom) {
79+
for (int j = left; j <= right; ++j) {
80+
ans.add(matrix[top][j]);
9181
}
92-
return;
93-
}
94-
if (j1 == j2) {
95-
for (int i = i1; i <= i2; ++i) {
96-
res.add(matrix[i][j1]);
82+
for (int i = top + 1; i <= bottom; ++i) {
83+
ans.add(matrix[i][right]);
9784
}
98-
return;
99-
}
100-
for (int j = j1; j < j2; ++j) {
101-
res.add(matrix[i1][j]);
102-
}
103-
for (int i = i1; i < i2; ++i) {
104-
res.add(matrix[i][j2]);
105-
}
106-
for (int j = j2; j > j1; --j) {
107-
res.add(matrix[i2][j]);
108-
}
109-
for (int i = i2; i > i1; --i) {
110-
res.add(matrix[i][j1]);
85+
if (left < right && top < bottom) {
86+
for (int j = right - 1; j >= left; --j) {
87+
ans.add(matrix[bottom][j]);
88+
}
89+
for (int i = bottom - 1; i > top; --i) {
90+
ans.add(matrix[i][left]);
91+
}
92+
}
93+
++top;
94+
--bottom;
95+
++left;
96+
--right;
11197
}
98+
return ans;
11299
}
113100
}
114101
```
@@ -121,40 +108,35 @@ class Solution {
121108
* @return {number[]}
122109
*/
123110
var spiralOrder = function (matrix) {
124-
let m = matrix.length;
125-
if (m === 0) return [];
126-
let res = [];
127-
let top = 0,
128-
bottom = m - 1,
129-
left = 0,
130-
right = matrix[0].length - 1;
131-
while (left < right && bottom > top) {
132-
for (let i = left; i < right; i++) res.push(matrix[top][i]);
133-
for (let i = top; i < bottom; i++) res.push(matrix[i][right]);
134-
for (let i = right; i > left; i--) res.push(matrix[bottom][i]);
135-
for (let i = bottom; i > top; i--) res.push(matrix[i][left]);
136-
top++;
137-
bottom--;
138-
left++;
139-
right--;
140-
}
141-
if (left === right) {
142-
for (i = top; i <= bottom; i++) res.push(matrix[i][left]);
143-
} else if (top === bottom) {
144-
for (i = left; i <= right; i++) res.push(matrix[top][i]);
111+
const m = matrix.length;
112+
const n = matrix[0].length;
113+
let [top, bottom, left, right] = [0, m - 1, 0, n - 1];
114+
let ans = [];
115+
while (top <= bottom && left <= right) {
116+
for (let j = left; j <= right; ++j) {
117+
ans.push(matrix[top][j]);
118+
}
119+
for (let i = top + 1; i <= bottom; ++i) {
120+
ans.push(matrix[i][right]);
121+
}
122+
if (left < right && top < bottom) {
123+
for (let j = right - 1; j >= left; --j) {
124+
ans.push(matrix[bottom][j]);
125+
}
126+
for (let i = bottom - 1; i > top; --i) {
127+
ans.push(matrix[i][left]);
128+
}
129+
}
130+
[top, bottom, left, right] = [top + 1, bottom - 1, left + 1, right - 1];
145131
}
146-
return res;
132+
return ans;
147133
};
148134
```
149135

150136
### **Go**
151137

152138
```go
153139
func spiralOrder(matrix [][]int) []int {
154-
if len(matrix) == 0 {
155-
return []int{}
156-
}
157-
158140
m, n := len(matrix), len(matrix[0])
159141
ans := make([]int, 0, m*n)
160142

@@ -190,105 +172,64 @@ func spiralOrder(matrix [][]int) []int {
190172
class Solution {
191173
public:
192174
vector<int> spiralOrder(vector<vector<int>>& matrix) {
193-
int row=matrix.size();
194-
if(row==0)
175+
int m = matrix.size(), n = matrix[0].size();
176+
int top = 0, bottom = m - 1, left = 0, right = n - 1;
177+
vector<int> ans;
178+
while (top <= bottom && left <= right)
195179
{
196-
vector<int> zero;
197-
zero.clear();
198-
return zero;
199-
}
200-
int col=matrix[0].size();
201-
if(row==1)
202-
return matrix[0];
203-
if(col==0)
204-
{
205-
vector<int> zero;
206-
zero.clear();
207-
return zero;
208-
}
209-
if(col==1)
210-
{
211-
vector<int> temp;
212-
for(int i=0;i<row;i++)
213-
temp.push_back(matrix[i][0]);
214-
return temp;
215-
}
216-
vector<int> result;
217-
result=matrix[0];//result存储第一行
218-
//temp=matirx.pop_back();
219-
for(int i=1;i<matrix.size()-1;i++)
220-
{
221-
result.push_back(matrix[i][matrix[0].size()-1]);//存储每行最后一个
222-
}
223-
for(int i=0;i<col;i++)
224-
{
225-
result.push_back(matrix[row-1][col-1-i]);//倒序存储最后一行
226-
}
227-
for(int i=1;i<row-1;i++)
228-
{
229-
result.push_back(matrix[row-i-1][0]);//倒序存储每一行第一个
230-
vector<int> zz(matrix[row-i-1].begin()+1,matrix[row-i-1].end()-1);//将中间行去除第一个和最后一个数
231-
matrix[row-i-1]=zz;
180+
for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]);
181+
for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]);
182+
if (left < right && top < bottom)
183+
{
184+
for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]);
185+
for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]);
186+
}
187+
++top;
188+
--bottom;
189+
++left;
190+
--right;
232191
}
233-
vector<vector<int>> m2(matrix.begin()+1,matrix.end()-1);//将matrix去除第一行和最后一行,递归调用
234-
//cout<<m2.size()<<" "<<m2[0].size();
235-
vector<int> l=spiralOrder(m2);
236-
result.insert(result.end(),l.begin(),l.end());//将递归结果插入result后面
237-
return result;
238-
192+
return ans;
239193
}
240194
};
241195
```
242196
243197
### **C#**
244198
245199
```cs
246-
using System;
247-
using System.Collections.Generic;
248-
249200
public class Solution {
250201
public IList<int> SpiralOrder(int[][] matrix) {
251-
var lenI = matrix.Length;
252-
var lenJ = lenI == 0 ? 0 : matrix[0].Length;
253-
var result = new List<int>(lenI * lenJ);
254-
var rounds = (Math.Min(lenI, lenJ) + 1) / 2;
255-
for (var r = 0; r < rounds; ++r)
202+
int m = matrix.Length;
203+
int n = matrix[0].Length;
204+
int top = 0, bottom = m - 1, left = 0, right = n - 1;
205+
var ans = new List<int>(m * n);
206+
while (top <= bottom && left <= right)
256207
{
257-
if (lenI - r * 2 == 1)
208+
for (int j = left; j <= right; ++j)
258209
{
259-
for (var j = r; j < lenJ - r; ++j)
260-
{
261-
result.Add(matrix[r][j]);
262-
}
210+
ans.Add(matrix[top][j]);
263211
}
264-
else if (lenJ - r * 2 == 1)
212+
for (int i = top + 1; i <= bottom; ++i)
265213
{
266-
for (var i = r; i < lenI - r; ++i)
267-
{
268-
result.Add(matrix[i][r]);
269-
}
214+
ans.Add(matrix[i][right]);
270215
}
271-
else
216+
if (left < right && top < bottom)
272217
{
273-
for (var j = r; j < lenJ - r - 1; ++j)
274-
{
275-
result.Add(matrix[r][j]);
276-
}
277-
for (var i = r; i < lenI - r - 1; ++i)
278-
{
279-
result.Add(matrix[i][lenJ - r - 1]);
280-
}
281-
for (var j = lenJ - r - 1; j > r; --j)
218+
for (int j = right - 1; j >= left; --j)
282219
{
283-
result.Add(matrix[lenI - r - 1][j]);
220+
ans.Add(matrix[bottom][j]);
284221
}
285-
for (var i = lenI - r - 1; i > r; --i)
222+
for (int i = bottom - 1; i > top; --i)
286223
{
287-
result.Add(matrix[i][r]);
224+
ans.Add(matrix[i][left]);
288225
}
289226
}
227+
++top;
228+
--bottom;
229+
++left;
230+
--right;
290231
}
291-
return result;
232+
return ans;
292233
}
293234
}
294235
```

0 commit comments

Comments
 (0)