您的位置:首页 > 其它

Leetcode#59||Spiral Matrix II

2015-08-17 18:14 417 查看
public class Solution {
public int[][] generateMatrix(int n) {
int[][] result = new int

;

int t= 0;
int cnt = 1;

while (t < n / 2) {
// top
for (int j = t; j < n - t - 1; j++) {
result[t][j] = cnt++;
}
// right
for (int i = t; i < n - t - 1; i++) {
result[i][n - t - 1] = cnt++;
}
// bottom
for (int j = n - t - 1; j > t; j--) {
result[n - t - 1][j] = cnt++;
}
// left
for (int i = n - t - 1; i > t; i--) {
result[i][t] = cnt++;
}

t++;
}

if (n % 2 == 1) {
result[n / 2][n / 2] = cnt;
}

return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Array