|
| 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 | +``` |
0 commit comments