Skip to content

Commit 329780e

Browse files
committed
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题29. 顺时针打印矩阵
1 parent a8f89ec commit 329780e

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [面试题29. 顺时针打印矩阵](https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/)
2+
3+
## 题目描述
4+
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
5+
6+
**示例 1:**
7+
8+
```
9+
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
10+
输出:[1,2,3,6,9,8,7,4,5]
11+
```
12+
13+
**示例 2:**
14+
15+
```
16+
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
17+
输出:[1,2,3,4,8,12,11,10,9,5,6,7]
18+
```
19+
20+
**限制:**
21+
22+
- `0 <= matrix.length <= 100`
23+
- `0 <= matrix[i].length <= 100`
24+
25+
## 解法
26+
### Python3
27+
```python
28+
class Solution:
29+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
30+
if len(matrix) == 0:
31+
return []
32+
33+
s1, e1, s2, e2 = 0, 0, len(matrix) - 1, len(matrix[0]) - 1
34+
res = []
35+
while s1 <= s2 and e1 <= e2:
36+
res += self._spiral_add(matrix, s1, e1, s2, e2)
37+
s1, e1, s2, e2 = s1 + 1, e1 + 1, s2 - 1, e2 - 1
38+
return res
39+
40+
def _spiral_add(self, matrix, s1, e1, s2, e2) -> List[int]:
41+
if s1 == s2:
42+
return [matrix[s1][j] for j in range(e1, e2 + 1)]
43+
if e1 == e2:
44+
return [matrix[i][e1] for i in range(s1, s2 + 1)]
45+
return [matrix[s1][j] for j in range(e1, e2)] + \
46+
[matrix[i][e2] for i in range(s1, s2)] + \
47+
[matrix[s2][j] for j in range(e2, e1, -1)] + \
48+
[ matrix[i][e1] for i in range(s2, s1, -1)]
49+
50+
```
51+
52+
### Java
53+
```java
54+
class Solution {
55+
private int index;
56+
public int[] spiralOrder(int[][] matrix) {
57+
if (matrix.length == 0) {
58+
return new int[0];
59+
}
60+
index = 0;
61+
int m = matrix.length, n = matrix[0].length;
62+
int s1 = 0, e1 = 0, s2 = m - 1, e2 = n - 1;
63+
int[] res = new int[m * n];
64+
while (s1 <= s2 && e1 <= e2) {
65+
spiralAdd(matrix, s1++, e1++, s2--, e2--, res);
66+
}
67+
return res;
68+
}
69+
70+
public void spiralAdd(int[][] matrix, int s1, int e1, int s2, int e2, int[] res) {
71+
if (s1 == s2) {
72+
for (int j = e1; j <= e2; ++j) {
73+
res[index++] = matrix[s1][j];
74+
}
75+
return;
76+
}
77+
if (e1 == e2) {
78+
for (int i = s1; i <= s2; ++i) {
79+
res[index++] = matrix[i][e1];
80+
}
81+
return;
82+
}
83+
84+
for (int j = e1; j < e2; ++j) {
85+
res[index++] = matrix[s1][j];
86+
}
87+
for (int i = s1; i < s2; ++i) {
88+
res[index++] = matrix[i][e2];
89+
}
90+
for (int j = e2; j > e1; --j) {
91+
res[index++] = matrix[s2][j];
92+
}
93+
for (int i = s2; i > s1; --i) {
94+
res[index++] = matrix[i][e1];
95+
}
96+
}
97+
}
98+
```
99+
100+
### ...
101+
```
102+
103+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
private int index;
3+
4+
public int[] spiralOrder(int[][] matrix) {
5+
if (matrix.length == 0) {
6+
return new int[0];
7+
}
8+
index = 0;
9+
int m = matrix.length, n = matrix[0].length;
10+
int s1 = 0, e1 = 0, s2 = m - 1, e2 = n - 1;
11+
int[] res = new int[m * n];
12+
while (s1 <= s2 && e1 <= e2) {
13+
spiralAdd(matrix, s1++, e1++, s2--, e2--, res);
14+
}
15+
return res;
16+
}
17+
18+
public void spiralAdd(int[][] matrix, int s1, int e1, int s2, int e2, int[] res) {
19+
if (s1 == s2) {
20+
for (int j = e1; j <= e2; ++j) {
21+
res[index++] = matrix[s1][j];
22+
}
23+
return;
24+
}
25+
if (e1 == e2) {
26+
for (int i = s1; i <= s2; ++i) {
27+
res[index++] = matrix[i][e1];
28+
}
29+
return;
30+
}
31+
32+
for (int j = e1; j < e2; ++j) {
33+
res[index++] = matrix[s1][j];
34+
}
35+
for (int i = s1; i < s2; ++i) {
36+
res[index++] = matrix[i][e2];
37+
}
38+
for (int j = e2; j > e1; --j) {
39+
res[index++] = matrix[s2][j];
40+
}
41+
for (int i = s2; i > s1; --i) {
42+
res[index++] = matrix[i][e1];
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
3+
if len(matrix) == 0:
4+
return []
5+
6+
s1, e1, s2, e2 = 0, 0, len(matrix) - 1, len(matrix[0]) - 1
7+
res = []
8+
while s1 <= s2 and e1 <= e2:
9+
res += self._spiral_add(matrix, s1, e1, s2, e2)
10+
s1, e1, s2, e2 = s1 + 1, e1 + 1, s2 - 1, e2 - 1
11+
return res
12+
13+
def _spiral_add(self, matrix, s1, e1, s2, e2) -> List[int]:
14+
if s1 == s2:
15+
return [matrix[s1][j] for j in range(e1, e2 + 1)]
16+
if e1 == e2:
17+
return [matrix[i][e1] for i in range(s1, s2 + 1)]
18+
return [matrix[s1][j] for j in range(e1, e2)] + \
19+
[matrix[i][e2] for i in range(s1, s2)] + \
20+
[matrix[s2][j] for j in range(e2, e1, -1)] + \
21+
[ matrix[i][e1] for i in range(s2, s1, -1)]

0 commit comments

Comments
 (0)