题目描述
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
代码
public static int[][] generateMatrix(int n) {
if (n <= 0) {
return new int[0][0];
}
int[][] matrix = new int[n][n];
int num = 1;
int startx = 0, endx = n - 1;
int starty = 0, endy = n - 1;
while (startx <= endx && starty <= endy) {
for (int y = starty; y <= endy; y++) {
matrix[startx][y] = num++;
}
for (int x = startx + 1; x <= endx; x++) {
matrix[x][endy] = num++;
}
if (startx == endx || starty == endy) {
break;
}
for (int y = endy - 1; y >= starty; y--) {
matrix[endx][y] = num++;
}
for (int x = endx - 1; x >= startx + 1; x--) {
matrix[x][starty] = num++;
}
startx++;
starty++;
endx--;
endy--;
}
return matrix;
}