lintcode-螺旋矩阵 II
来源:互联网 发布:哪些是淘宝禁售品 编辑:程序博客网 时间:2024/06/01 09:19
给你一个数n生成一个包含1-n^2的螺旋形矩阵
样例
n = 3
矩阵为
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
class Solution {public: /** * @param n an integer * @return a square matrix */ vector<vector<int> > generateMatrix(int n) { // Write your code here vector<vector<int> > vec(n,vector<int>(n,0)); if(n<=0) return vec; int row=n;//行数 int col=n;//列数 int left=0,top=0;//左上角坐标(left,top) int bottom=row-1,right=col-1;//右下角坐标(bottom,right) int k=1; while(left<=bottom && top<=right) { for(int i=top;i<=right;i++) vec[left][i]=k++;//从左到右打印 for(int i=left+1;i<=bottom;i++) vec[i][right]=k++;//从上到下打印 if(left!=bottom) for(int i=right-1;i>=top;i--) vec[bottom][i]=k++;//从右往左打印 if(top!=right) for(int i=bottom-1;i>left;i--) vec[i][top]=k++;//从左向上打印 left++; top++; bottom--; right--; } return vec; }};
0 0
- lintcode-螺旋矩阵 II
- Lintcode 螺旋矩阵 II
- lintcode --螺旋矩阵II
- 螺旋矩阵 II -LintCode
- [LintCode]381.螺旋矩阵 II
- [LintCode] 螺旋矩阵II Spiral Matrix II
- lintcode,螺旋矩阵
- [LintCode]374.螺旋矩阵
- 螺旋矩阵-LintCode
- 螺旋矩阵II
- 螺旋矩阵 II
- [LintCode] 螺旋矩阵 Spiral Matrix
- *[Lintcode]Spiral Matrix螺旋矩阵
- LintCode 374-螺旋矩阵 状态机
- LintCode 螺旋矩阵 (新解法)
- LintCode-剑指Offer-(374)螺旋矩阵
- LintCode 374 螺旋矩阵的解决方法
- lintcode刷题——螺旋矩阵
- Rotate Image
- 运用 三种 原生 谷歌 阿里 解析和生成json
- 对称加密和非对称加密
- 通过Controller返回到jsp页面后,图片、css样式等消失了
- iOS扬声器和听筒模式的切换以及距离传感器红外感应的设置
- lintcode-螺旋矩阵 II
- activemq安全设置—设置admin的用户名和密码
- Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompa
- 25. Reverse Nodes in k-Group(23.53%)
- EventBus使用详情、源码分析和注意事项
- 关于RemoteView的一点愚见(实现桌面小部件)
- 深入 HBase 架构解析(1)
- QtWebEngine打包时与众不同的地方
- Struct与class的区别