|
18 | 18 | public class _59 {
|
19 | 19 |
|
20 | 20 | public static class Solution1 {
|
21 |
| - public int[][] generateMatrix(int num) { |
22 |
| - int temp = num; |
23 |
| - int[][] fourEdges = new int[num][num]; |
| 21 | + /**credit: https://leetcode.com/problems/spiral-matrix-ii/discuss/22289/My-Super-Simple-Solution.-Can-be-used-for-both-Spiral-Matrix-I-and-II/21907*/ |
| 22 | + public int[][] generateMatrix(int n) { |
| 23 | + int[][] matrix = new int[n][n]; |
| 24 | + if (n == 0) { |
| 25 | + return matrix; |
| 26 | + } |
24 | 27 | int value = 1;
|
25 |
| - int i = 0; |
26 |
| - int j = 0; |
27 |
| - if (num % 2 == 0) { |
28 |
| - //when num is even |
29 |
| - while (i < num / 2 && j < num / 2 && temp >= 0) { |
30 |
| - /* Assign the top row */ |
31 |
| - while (j < temp) { |
32 |
| - fourEdges[i][j] = value; |
33 |
| - j++; |
34 |
| - value++; |
35 |
| - } |
36 |
| - |
37 |
| - /* Assign the right column */ |
38 |
| - while (i < temp - 1) { |
39 |
| - i++; |
40 |
| - fourEdges[i][j - 1] = value; |
41 |
| - value++; |
42 |
| - } |
43 |
| - j = j - 2; |
44 |
| - |
45 |
| - /* Assign the bottom row */ |
46 |
| - while (j >= num - temp) { |
47 |
| - fourEdges[i][j] = value; |
48 |
| - j--; |
49 |
| - value++; |
50 |
| - } |
51 |
| - i--; |
52 |
| - j++; |
53 |
| - |
54 |
| - /* Assign the left column */ |
55 |
| - while (i > num - temp) { |
56 |
| - fourEdges[i][j] = value; |
57 |
| - i--; |
58 |
| - value++; |
59 |
| - } |
60 |
| - //} |
61 |
| - i++; |
62 |
| - j++; |
63 |
| - temp--; |
| 28 | + int top = 0; |
| 29 | + int bottom = n - 1; |
| 30 | + int left = 0; |
| 31 | + int right = n - 1; |
| 32 | + while (left <= right && top <= bottom) { |
| 33 | + for (int j = left; j <= right; j++) { |
| 34 | + matrix[top][j] = value++; |
64 | 35 | }
|
65 |
| - } else { |
66 |
| - //when num is odd |
67 |
| - while (i < num / 2 && j < num / 2 && temp >= 0) { |
68 |
| - /* Assign the top row */ |
69 |
| - while (j < temp) { |
70 |
| - fourEdges[i][j] = value; |
71 |
| - j++; |
72 |
| - value++; |
73 |
| - } |
74 |
| - |
75 |
| - /* Assign the right column */ |
76 |
| - while (i < temp - 1) { |
77 |
| - i++; |
78 |
| - fourEdges[i][j - 1] = value; |
79 |
| - value++; |
80 |
| - } |
81 |
| - j = j - 2; |
82 |
| - |
83 |
| - /* Assign the bottom row */ |
84 |
| - while (j >= num - temp) { |
85 |
| - fourEdges[i][j] = value; |
86 |
| - j--; |
87 |
| - value++; |
88 |
| - } |
89 |
| - i--; |
90 |
| - j++; |
91 |
| - |
92 |
| - /* Assign the left column */ |
93 |
| - while (i > num - temp) { |
94 |
| - fourEdges[i][j] = value; |
95 |
| - i--; |
96 |
| - value++; |
97 |
| - } |
98 |
| - //} |
99 |
| - i++; |
100 |
| - j++; |
101 |
| - temp--; |
| 36 | + top++; |
| 37 | + for (int i = top; i <= bottom; i++) { |
| 38 | + matrix[i][right] = value++; |
102 | 39 | }
|
103 |
| - fourEdges[num / 2][num / 2] = num * num; |
104 |
| - } |
105 |
| - |
106 |
| - for (int m = 0; m < num; m++) { |
107 |
| - for (int n = 0; n < num; n++) { |
108 |
| - System.out.print(fourEdges[m][n] + "\t"); |
109 |
| - if ((n + 1) % num == 0) { |
110 |
| - System.out.println(); |
111 |
| - } |
| 40 | + right--; |
| 41 | + for (int j = right; j >= left; j--) { |
| 42 | + matrix[bottom][j] = value++; |
| 43 | + } |
| 44 | + bottom--; |
| 45 | + for (int i = bottom; i >= top; i--) { |
| 46 | + matrix[i][left] = value++; |
112 | 47 | }
|
| 48 | + left++; |
113 | 49 | }
|
114 |
| - return fourEdges; |
| 50 | + return matrix; |
115 | 51 | }
|
116 | 52 | }
|
117 | 53 | }
|
0 commit comments