//C语言/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/int**generateMatrix(int n,int* returnSize,int** returnColumnSizes){*returnSize=n;*returnColumnSizes=(int*)malloc(sizeof(int)*n);//初始化最终的结果数组int** ans=(int**)malloc(sizeof(int*)* n);int i;for(i=0;i<n;i++){
ans[i]=(int*)malloc(sizeof(int)* n);(*returnColumnSizes)[i]= n;}int startx=0;int starty=0;int offset=1;//偏移数int count=1;int mid=n/2;//最中间的元素int loop=n/2;//循环圈数while(loop){int i=startx;int j=starty;//第一排从左到右,左闭右开for(;j<starty+n-offset;j++){
ans[startx][j]=count;
count++;}//最后一列从上到下for(;i<startx+n-offset;i++){
ans[i][j]=count;
count++;}//最后一行从右到左for(;j>starty;j--){
ans[i][j]=count;
count++;}//第一列从下到上for(;i>startx;i--){
ans[i][j]=count;
count++;}
offset+=2;
startx++;
starty++;
loop--;}if(n%2){
ans[mid][mid]=count;}return ans;}