059.Spiral Matrix II

Solution 1: accepted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static int[][] generateMatrix(int n) {
int[][] ret = new int[n][n];
int left = 0,top = 0;
int right = n -1,down = n - 1;
int count = 1;
while (left <= right) {
for (int j = left; j <= right; j ++) {
ret[top][j] = count++;
}
top ++;
for (int i = top; i <= down; i ++) {
ret[i][right] = count ++;
}
right --;
for (int j = right; j >= left; j --) {
ret[down][j] = count ++;
}
down --;
for (int i = down; i >= top; i --) {
ret[i][left] = count ++;
}
left ++;
}
return ret;
}