Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n =
You should return the following matrix:Given n =
3
,[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]Solution:O(n^2)
public class Solution {
public int[][] generateMatrix(int n) {
if(n<0) return null;
int[][] ret = new int[n][n];
int start=1;
int x=0;
int y=0;
for(int i=n;i>0;i-=2){
if(i==1){
ret[x][y]=start;
}else{
for(int j=0;j<i-1;j++){
ret[x][y++]=start++;
}
for(int j=0;j<i-1;j++){
ret[x++][y]=start++;
}
for(int j=0;j<i-1;j++){
ret[x][y--]=start++;
}
for(int j=0;j<i-1;j++){
ret[x--][y]=start++;
}
x++;
y++;
}
}
return ret;
}
}
沒有留言:
張貼留言