您的位置:首页 > 其它

59. Spiral Matrix II

2017-08-22 11:41 204 查看
/*
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 ]
]

*/

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n,vector<int>(n));
int row_u = 0,row_d=n-1,column_l=0,column_r=n-1;
int k=1;
while(k <= n*n)
{
for(int i=column_l;i<=column_r;i++)//上面一行
res[row_u][i]=k++;
row_u++;
for(int i=row_u;i<=row_d;i++)//右边一列
res[i][column_r]=k++;
column_r--;
for(int i=column_r;i>=column_l;i--)//下面一行
res[row_d][i]=k++;
row_d--;
for(int i=row_d;i>=row_u;i--)//左边一列
res[i][column_l]=k++;
column_l++;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode array matrix