59. Spiral Matrix II
2016-07-16 10:57
155 查看
Given an integer n, generate a square matrix filled with elements from 1 to
n2 in spiral order.
For example,
Given n =
You should return the following matrix:
Subscribe to see which companies asked this question
n2 in spiral order.
For example,
Given n =
3,
You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
Subscribe to see which companies asked this question
class Solution { public: void fillvec(vector<vector<int>>& v1,int start,int len,int &num) { int i=0,j=0; while(j<len) { v1[start+i][j+start]=num++; j++; } j-=1; i+=1; while(i<len) { v1[start+i][j+start]=num++; i++; } i-=1; j-=1; while(j>=0) { v1[start+i][j+start]=num++; j--; } j+=1; i-=1; while(i>0) { v1[start+i][j+start]=num++; i--; } return ; } vector<vector<int>> generateMatrix(int n) { vector<vector<int>> v1(n,vector<int>(n,0)); int j=0; int num=1; for(int i=0;i<n;i+=2) fillvec(v1,j++,n-i,num); return v1; } };
相关文章推荐
- JS的DOM操作
- 初涉HTML(3)
- JAVA使用数组简单模拟队列
- Composer的简单安装与使用
- main函数执行前、后再执行的代码
- Zookeeper全解析——Paxos作为灵魂
- 在stm32中使用while循环导致程序卡死
- C#简单多线程Demo
- 在BIOS中开启虚拟化技术
- OTT TV 与 IPTV 的区别
- PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令
- long和int的区别
- Linux 虚拟内存和物理内存的管理
- HDU 1582 AC Again有趣的搜索
- CentOS 安装Nginx
- (7) ebj学习: jpa 一对一,一对多,多对多
- TreeMap get获取数据为null
- 中国七大新闻机构联合设立中国搜索
- 如何让Linux定时任务crond以秒为单位执行(如每隔3秒)
- OpenStack和CloudStack对比研究报告