|
25 | 25 |
|
26 | 26 | ## 解法
|
27 | 27 |
|
| 28 | +从外往里一圈一圈遍历并存储矩阵元素即可。 |
| 29 | + |
28 | 30 | <!-- tabs:start -->
|
29 | 31 |
|
30 | 32 | ### **Python3**
|
31 | 33 |
|
32 | 34 | ```python
|
33 | 35 | class Solution:
|
34 | 36 | def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
|
35 |
| - if len(matrix) == 0: |
| 37 | + def add(i1, j1, i2, j2): |
| 38 | + if i1 == i2: |
| 39 | + return [matrix[i1][j] for j in range(j1, j2 + 1)] |
| 40 | + if j1 == j2: |
| 41 | + return [matrix[i][j1] for i in range(i1, i2 + 1)] |
| 42 | + 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)] |
| 43 | + if not matrix or not matrix[0]: |
36 | 44 | return []
|
37 |
| - |
38 |
| - s1, e1, s2, e2 = 0, 0, len(matrix) - 1, len(matrix[0]) - 1 |
| 45 | + m, n = len(matrix), len(matrix[0]) |
| 46 | + i1, j1, i2, j2 = 0, 0, m - 1, n - 1 |
39 | 47 | res = []
|
40 |
| - while s1 <= s2 and e1 <= e2: |
41 |
| - res += self._spiral_add(matrix, s1, e1, s2, e2) |
42 |
| - s1, e1, s2, e2 = s1 + 1, e1 + 1, s2 - 1, e2 - 1 |
| 48 | + while i1 <= i2 and j1 <= j2: |
| 49 | + res += add(i1, j1, i2, j2) |
| 50 | + i1, j1, i2, j2 = i1 + 1, j1 + 1, i2 - 1, j2 - 1 |
43 | 51 | return res
|
44 |
| - |
45 |
| - def _spiral_add(self, matrix, s1, e1, s2, e2) -> List[int]: |
46 |
| - if s1 == s2: |
47 |
| - return [matrix[s1][j] for j in range(e1, e2 + 1)] |
48 |
| - if e1 == e2: |
49 |
| - return [matrix[i][e1] for i in range(s1, s2 + 1)] |
50 |
| - return [matrix[s1][j] for j in range(e1, e2)] + \ |
51 |
| - [matrix[i][e2] for i in range(s1, s2)] + \ |
52 |
| - [matrix[s2][j] for j in range(e2, e1, -1)] + \ |
53 |
| - [matrix[i][e1] for i in range(s2, s1, -1)] |
54 |
| - |
55 | 52 | ```
|
56 | 53 |
|
57 | 54 | ### **Java**
|
58 | 55 |
|
59 | 56 | ```java
|
60 | 57 | class Solution {
|
| 58 | + private int[] res; |
61 | 59 | private int index;
|
| 60 | + |
62 | 61 | public int[] spiralOrder(int[][] matrix) {
|
63 |
| - if (matrix.length == 0) { |
64 |
| - return new int[0]; |
65 |
| - } |
| 62 | + int m, n; |
| 63 | + if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) |
| 64 | + return new int[]{}; |
| 65 | + res = new int[m * n]; |
66 | 66 | index = 0;
|
67 |
| - int m = matrix.length, n = matrix[0].length; |
68 |
| - int s1 = 0, e1 = 0, s2 = m - 1, e2 = n - 1; |
69 |
| - int[] res = new int[m * n]; |
70 |
| - while (s1 <= s2 && e1 <= e2) { |
71 |
| - spiralAdd(matrix, s1++, e1++, s2--, e2--, res); |
| 67 | + int i1 = 0, i2 = m - 1; |
| 68 | + int j1 = 0, j2 = n - 1; |
| 69 | + while (i1 <= i2 && j1 <= j2) { |
| 70 | + add(matrix, i1++, j1++, i2--, j2--); |
72 | 71 | }
|
73 | 72 | return res;
|
74 | 73 | }
|
75 | 74 |
|
76 |
| - public void spiralAdd(int[][] matrix, int s1, int e1, int s2, int e2, int[] res) { |
77 |
| - if (s1 == s2) { |
78 |
| - for (int j = e1; j <= e2; ++j) { |
79 |
| - res[index++] = matrix[s1][j]; |
| 75 | + private void add(int[][] matrix, int i1, int j1, int i2, int j2) { |
| 76 | + if (i1 == i2) { |
| 77 | + for (int j = j1; j <= j2; ++j) { |
| 78 | + res[index++] = matrix[i1][j]; |
80 | 79 | }
|
81 | 80 | return;
|
82 | 81 | }
|
83 |
| - if (e1 == e2) { |
84 |
| - for (int i = s1; i <= s2; ++i) { |
85 |
| - res[index++] = matrix[i][e1]; |
| 82 | + if (j1 == j2) { |
| 83 | + for (int i = i1; i <= i2; ++i) { |
| 84 | + res[index++] = matrix[i][j1]; |
86 | 85 | }
|
87 | 86 | return;
|
88 | 87 | }
|
89 |
| - |
90 |
| - for (int j = e1; j < e2; ++j) { |
91 |
| - res[index++] = matrix[s1][j]; |
| 88 | + for (int j = j1; j < j2; ++j) { |
| 89 | + res[index++] = matrix[i1][j]; |
92 | 90 | }
|
93 |
| - for (int i = s1; i < s2; ++i) { |
94 |
| - res[index++] = matrix[i][e2]; |
| 91 | + for (int i = i1; i < i2; ++i) { |
| 92 | + res[index++] = matrix[i][j2]; |
95 | 93 | }
|
96 |
| - for (int j = e2; j > e1; --j) { |
97 |
| - res[index++] = matrix[s2][j]; |
| 94 | + for (int j = j2; j > j1; --j) { |
| 95 | + res[index++] = matrix[i2][j]; |
98 | 96 | }
|
99 |
| - for (int i = s2; i > s1; --i) { |
100 |
| - res[index++] = matrix[i][e1]; |
| 97 | + for (int i = i2; i > i1; --i) { |
| 98 | + res[index++] = matrix[i][j1]; |
101 | 99 | }
|
102 | 100 | }
|
103 | 101 | }
|
|
0 commit comments