您的位置:首页 > 其它

Leetcode 59. Spiral Matrix II

2016-12-31 11:46 375 查看
public class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int

;
int up = 0, right = n-1, down = n-1, left = 0, elem = 1;

while (true) {
for (int col=left; col<=right; col++) res[up][col] = elem++;
if (++up > down) break;

for (int row=up; row<=down; row++) res[row][right] = elem++;
if (--right < left) break;

for (int col=right; col>=left; col--) res[down][col] = elem++;
if (--down < up) break;

for (int row=down; row>=up; row--) res[row][left] = elem++;
if (++left > right) break;
}

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