码迷,mamicode.com
首页 > 其他好文 > 详细

59. Spiral Matrix II

时间:2018-07-18 13:53:44      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:color   generate   rate   style   ++   res   cto   while   tor   

 1 class Solution 
 2 {
 3 public:
 4     vector<vector<int>> generateMatrix(int n) 
 5     {
 6         vector<vector<int>> res(n,vector<int> (n,0));
 7         int right=n-1,down=n-1;
 8         int left=0,up=0,k=1;
 9         int lim=n*n;
10         while(k<=lim)
11         {
12             for(int x=left;x<=right;x++)
13                 res[up][x]=k++;
14             up++;
15             for(int y=up;y<=down;y++)
16                 res[y][right]=k++;
17             right--;
18             for(int x=right;x>=left;x--)
19                 res[down][x]=k++;
20             down--;
21             for(int y=down;y>=up;y--)
22                 res[y][left]=k++;
23             left++;
24         }
25         return res;
26     }
27 };

和螺旋输出相反,这个直接螺旋填写即可

59. Spiral Matrix II

标签:color   generate   rate   style   ++   res   cto   while   tor   

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9328433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!