Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ] ]
You should return [1,2,3,6,9,8,7,4,5]
.
代码:
class Solution{ public:void doit(int start,int row,int col,vector<int>& res,vector<vector<int> > matrix){}vector<int> spiralOrder(vector<vector<int> > &matrix) {vector<int> res;if(matrix.empty()) return res;int row=matrix.size()-1;int col=matrix[0].size()-1;if(row==-1||col==-1) return res;int start=0;int s=min(row,col);while(row>=0&&col>=0&&start<=s){if(res.size()==(matrix.size()*matrix[0].size())) break;if(row==start&&col==start) {res.push_back(matrix[start][start]);return res;}//只有一个数字if(row==start){//只有一行for (int i=start;i<=col;++i)res.push_back(matrix[start][i]);break;}if(col==start){//只有一列for (int i=start;i<=row;++i)res.push_back(matrix[i][start]);break;}for (int i=start;i<=col;++i) res.push_back(matrix[start][i]);for (int j=start+1;j<=row;++j) res.push_back(matrix[j][col]);for (int m=col-1;m>=start;--m) res.push_back(matrix[row][m]);for (int n=row-1;n>=start+1;--n) res.push_back(matrix[n][start]);++start;--row;--col;}return res;} };
转载于:https://www.cnblogs.com/fightformylife/p/4224238.html
Spiral Matrix相关推荐
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- LeetCode Spiral Matrix II (生成螺旋矩阵)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- 59. Spiral Matrix II
/** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...
- Spiral Matrix I II
Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...
- C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...
- PAT1105:Spiral Matrix
1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode 54. Spiral Matrix
54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the m ...
最新文章
- 今天来说一下我的SAMC
- 9于word没有注册类_XPage系列这次升级后终于是全自动化注册了!
- 银行现在都很缺钱吗,为什么给的利息比以前高了?
- .NET 异步详解(更新)
- 合成/聚合原则: 桥接模式
- java 标准输入流 关闭 打开_java--标准输入输出流
- 让数据中台飞起来—— Quick BI性能优化解决方案及实践
- 如何选择开源许可证?
- 限制UI只能在屏幕内移动(放大或缩小屏幕同样适用)
- c++获得cpu厂商_使用Visual C++编程取得CPU信息
- CCF CSP 201703-1 分蛋糕
- 西交大计算机系分数线,西安交通大学专业排名及分数线
- 创业公司失败的20大原因 没市场需求排第一
- 我的世界java版刷铁机_我的世界:教你建造新版本中最简单的刷铁机,效率高人人学的会...
- Java实例化类的几种方法
- 计算机开机时间设置方法,win10系统设置电脑开关机时间的技巧介绍
- sklearn笔记19 随机森林和决策树的比较
- ISP 接口隔离原则 Interface Seperate Principle
- GitHub与微信开启“秘密扫描”计划,来确保数据安全
- 世界上50大开心的事
热门文章
- SparkSQL ThriftServer 安全相关功能的现状分析
- 读取EXCEL文件内容
- 《PowerShell V3——SQL Server 2012数据库自动化运维权威指南》——2.8 创建数据库...
- 《深入理解Spark:核心思想与源码分析》——第1章环境准备
- linux内存cached手动释放
- DP之最优二叉查找树
- WinXP下At,Schtask和Eventtriggers命令实例(一)
- WPF程序中的XPSDocumentViewer
- log4j2的配置文件log4j2.xml笔记
- Office 365系列(7)------ Exchange 2013与Office 365 Hybrid混合部署Step by Step参考