59. Spiral Matrix II
2016-09-01 13:30
197 查看
class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> res(n,vector<int>(n)); int r=n-1,c=n-1,cnt=1; for(int x=0,y=0,i=0,j=0;x<=r&&y<=c;x++,y++) { for(j=y;j<=c;j++) { res[x][j]=cnt++; } for(i=x+1;i<=r;i++) { res[i][c]=cnt++; } for(j=c-1;j>=x&&x!=r;j--) { res[r][j]=cnt++; } for(i=r-1;i>x&&y!=c;i--) { res[i][y]=cnt++; } r--; c--; } return res; } };
相关文章推荐
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- 【leetcode】59. Spiral Matrix II
- 59. Spiral Matrix II
- 59. Spiral Matrix II
- leetcode——59——Spiral Matrix II
- 59. Spiral Matrix II
- LeetCode 59 --- Spiral Matrix II
- 59. Spiral Matrix II
- Leetcode 59 Spiral Matrix II
- LeetCode 59: Spiral Matrix II
- leetcode 59. Spiral Matrix II
- Leetcode59 Spiral Matrix II
- 59. Spiral Matrix II
- 54. Spiral Matrix&59. Spiral Matrix II
- [C语言][LeetCode][59]Spiral Matrix II
- 59. Spiral Matrix II
- LeetCode54/59 Spiral Matrix I/II
- [LeetCode OJ]59. Spiral Matrix II