|
9 | 9 | */ |
10 | 10 | public class Solution { |
11 | 11 |
|
12 | | - public static int[] spiralOrder(int[][] matrix) { |
13 | | - if (matrix.length == 0) { |
14 | | - return new int[0]; |
15 | | - } |
16 | | - int[] res = new int[matrix.length * matrix[0].length]; |
17 | | - int u = 0, d = matrix.length - 1, l = 0, r = matrix[0].length - 1; |
18 | | - int idx = 0; |
19 | | - while (true) { |
20 | | - for (int i = l; i <= r; i++) { |
21 | | - res[idx++] = matrix[u][i]; |
22 | | - } |
23 | | - if (++u > d) { |
24 | | - break; |
25 | | - } |
26 | | - for (int i = u; i <= d; i++) { |
27 | | - res[idx++] = matrix[i][r]; |
28 | | - } |
29 | | - if (--r < l) { |
30 | | - break; |
31 | | - } |
32 | | - for (int i = r; i >= l; i--) { |
33 | | - res[idx++] = matrix[d][i]; |
34 | | - } |
35 | | - if (--d < u) { |
36 | | - break; |
37 | | - } |
38 | | - for (int i = d; i >= u; i--) { |
39 | | - res[idx++] = matrix[i][l]; |
40 | | - } |
41 | | - if (++l > r) { |
42 | | - break; |
43 | | - } |
44 | | - } |
45 | | - return res; |
46 | | - } |
| 12 | + //public static int[] spiralOrder(int[][] matrix) { |
| 13 | + // if (matrix.length == 0) { |
| 14 | + // return new int[0]; |
| 15 | + // } |
| 16 | + // int[] res = new int[matrix.length * matrix[0].length]; |
| 17 | + // int u = 0, d = matrix.length - 1, l = 0, r = matrix[0].length - 1; |
| 18 | + // int idx = 0; |
| 19 | + // while (true) { |
| 20 | + // for (int i = l; i <= r; i++) { |
| 21 | + // res[idx++] = matrix[u][i]; |
| 22 | + // } |
| 23 | + // if (++u > d) { |
| 24 | + // break; |
| 25 | + // } |
| 26 | + // for (int i = u; i <= d; i++) { |
| 27 | + // res[idx++] = matrix[i][r]; |
| 28 | + // } |
| 29 | + // if (--r < l) { |
| 30 | + // break; |
| 31 | + // } |
| 32 | + // for (int i = r; i >= l; i--) { |
| 33 | + // res[idx++] = matrix[d][i]; |
| 34 | + // } |
| 35 | + // if (--d < u) { |
| 36 | + // break; |
| 37 | + // } |
| 38 | + // for (int i = d; i >= u; i--) { |
| 39 | + // res[idx++] = matrix[i][l]; |
| 40 | + // } |
| 41 | + // if (++l > r) { |
| 42 | + // break; |
| 43 | + // } |
| 44 | + // } |
| 45 | + // return res; |
| 46 | + //} |
| 47 | + // |
| 48 | + //private static int[] spiralOrder1(int[][] matrix) { |
| 49 | + // if (matrix == null || matrix.length == 0) return new int[0]; |
| 50 | + // |
| 51 | + // int numEle = matrix.length * matrix[0].length; |
| 52 | + // int[] result = new int[numEle]; |
| 53 | + // int idx = 0; |
| 54 | + // int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1; |
| 55 | + // |
| 56 | + // while (numEle >= 1) { |
| 57 | + // for (int i = left; i <= right && numEle >= 1; i++) { |
| 58 | + // result[idx++] = matrix[top][i]; |
| 59 | + // numEle--; |
| 60 | + // } |
| 61 | + // top++; |
| 62 | + // for (int i = top; i <= bottom && numEle >= 1; i++) { |
| 63 | + // result[idx++] = matrix[i][right]; |
| 64 | + // numEle--; |
| 65 | + // } |
| 66 | + // right--; |
| 67 | + // for (int i = right; i >= left && numEle >= 1; i--) { |
| 68 | + // result[idx++] = matrix[bottom][i]; |
| 69 | + // numEle--; |
| 70 | + // } |
| 71 | + // bottom--; |
| 72 | + // for (int i = bottom; i >= top && numEle >= 1; i--) { |
| 73 | + // result[idx++] = matrix[i][left]; |
| 74 | + // numEle--; |
| 75 | + // } |
| 76 | + // left++; |
| 77 | + // } |
| 78 | + // return result; |
| 79 | + //} |
47 | 80 |
|
48 | | - private static int[] spiralOrder1(int[][] matrix) { |
49 | | - if (matrix == null || matrix.length == 0) return new int[0]; |
50 | | - |
51 | | - int numEle = matrix.length * matrix[0].length; |
52 | | - int[] result = new int[numEle]; |
53 | | - int idx = 0; |
54 | | - int left = 0, right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1; |
55 | | - |
56 | | - while (numEle >= 1) { |
57 | | - for (int i = left; i <= right && numEle >= 1; i++) { |
58 | | - result[idx++] = matrix[top][i]; |
59 | | - numEle--; |
60 | | - } |
61 | | - top++; |
62 | | - for (int i = top; i <= bottom && numEle >= 1; i++) { |
63 | | - result[idx++] = matrix[i][right]; |
64 | | - numEle--; |
65 | | - } |
66 | | - right--; |
67 | | - for (int i = right; i >= left && numEle >= 1; i--) { |
68 | | - result[idx++] = matrix[bottom][i]; |
69 | | - numEle--; |
70 | | - } |
71 | | - bottom--; |
72 | | - for (int i = bottom; i >= top && numEle >= 1; i--) { |
73 | | - result[idx++] = matrix[i][left]; |
74 | | - numEle--; |
75 | | - } |
76 | | - left++; |
77 | | - } |
78 | | - return result; |
79 | | - } |
80 | | - |
81 | | - public static void main(String[] args) { |
82 | | - int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; |
83 | | - int[] res = spiralOrder1(matrix); |
84 | | - System.out.println(Arrays.toString(res)); |
85 | | - } |
| 81 | + //public static void main(String[] args) { |
| 82 | + // int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; |
| 83 | + // int[] res = spiralOrder1(matrix); |
| 84 | + // System.out.println(Arrays.toString(res)); |
| 85 | + //} |
86 | 86 |
|
87 | 87 | } |
0 commit comments