59. The spiral matrix II
Given a positive integer n, to generate a 1 n2 contains all of the elements, and the element arranged spirally clockwise order square matrix.
Example:
Input: 3
Output:
[
[1, 2, 3],
[8,9, 4],
[7, 6, 5]
]
class Solution {
public int[][] generateMatrix(int n) {
int[][] arr = new int[n][n];
int c = 1, j = 0;
while (c <= n * n) {
for (int i = j; i < n - j; i++)
arr[j][i] = c++;
for (int i = j + 1; i < n - j; i++)
arr[i][n - j - 1] = c++;
for (int i = n - j - 2; i >= j; i--)
arr[n - j - 1][i] = c++;
for (int i = n -j - 2; i > j; i--)
arr[i][j] = c++;
j++;
}
return arr;
}
}