【leetcode】Spiral Matrix II
2015-01-08 20:25
381 查看
Spiral Matrix II
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 ] ]
与Spiral Matrix类似:
class Solution { public: vector<vector<int> > generateMatrix(int n) { int x1=0; int y1=0; int x2=n-1; int y2=n-1; int count=0; vector<vector<int>> result(n,vector<int>(n)); while(count<n*n) { for(int j=y1;j<=y2;j++) { count++; result[x1][j]=count; } for(int i=x1+1;i<=x2;i++) { count++; result[i][y2]=count; } for(int j=y2-1;j>=y1;j--) { count++; result[x2][j]=count; } for(int i=x2-1;i>x1;i--) { count++; result[i][x1]=count; } x1++;y1++;x2--;y2--; } return result; } };
相关文章推荐
- Spiral Matrix II(leetcode)
- leetcode59-Spiral Matrix II(生成蛇形矩阵)
- Leetcode Spiral Matrix II
- LeetCode Spiral Matrix II
- Leetcode: Spiral Matrix II
- (Java)LeetCode-59. Spiral Matrix II
- LeetCode 059 Spiral Matrix II
- LeetCode--Spiral Matrix II
- [LeetCode]题解(python):059-Spiral Matrix II
- Leetcode NO.59 Spiral Matrix II
- LeetCode: Spiral Matrix II
- LeetCode之Spiral Matrix II
- 【LeetCode从零单刷】Spiral Matrix II
- [Leetcode] Spiral Matrix II
- Leetcode Spiral Matrix II
- LeetCode Spiral Matrix II
- LeetCode 59 Spiral Matrix II
- LeetCode Spiral Matrix II
- [LeetCode] Spiral Matrix II
- leetcode ----59. Spiral Matrix II