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 int[][] generateMatrix(int n) {
if(n<0)
return null;
int [][]result=new int[n][n];
int k=1;
int a=0;
int b=-1;//第一个会马上加到0的
int j;
int times=2*(n-1)+1;
for(int i=0;i
switch (i%4) {
case 0:
++b;
j=n-(i+1)/2;//计算马上要走的个数
for(int m=0;m
result[a][b]=k;
++k;
++b;
}
--b;//要回退
break;
case 1:
j=n-(i+1)/2;
++a;
for(int m=0;m
result[a][b]=k;
++k;
++a;
}
--a;
break;
case 2:
j=n-(i+1)/2;
--b;
for(int m=0;m
result[a][b]=k;
++k;
--b;
}
++b;
break;
default:
j=n-(i+1)/2;
--a;
for(int m=0;m
result[a][b]=k;
++k;
--a;
}
++a;
break;
}
}
return result;
}
阅读(194) | 评论(0) | 转发(0) |