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].

  注意几点: 1)spiral 总共转了几圈 2) 最后一圈的时候如果是“横”“竖”需要处理好边界

class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
 vector<int> res;
int rows = matrix.size();
if(rows == ) return res;
int columns = matrix[].size();
if( columns ==) return res; int lays = rows > columns ? (columns+)/ : (rows+) /;
bool single = rows > columns ? columns% :rows% ;
for(int lay = ; lay < lays ; lay++)
{
int topRow = lay;
int rightColumn = columns - - lay; //process the top row
for(int i = lay ; i <= rightColumn ; i++)
res.push_back(matrix[topRow][i]);
//process the right column ,not include the first of the right column element
for(int i = lay + ; i <= rows - - lay ; i++)
res.push_back(matrix[i][rightColumn]); if(lay == lays - && single)
continue ;
//process the bottom row, not include the last of the bottom row element
for(int i = rightColumn - ;i >= lay ; i--)
res.push_back(matrix[rows--lay][i]); //process the left;
for(int i = rows - - lay - ;i > lay ; i--)
res.push_back(matrix[i][lay]); } return res;
}
};

最新文章

  1. Spring 4 异常处理
  2. Oracle 在线重定义表分区
  3. CSS-清除浮动
  4. PS 切图
  5. mysql 内存相关文章
  6. Parametric Curves and Surfaces
  7. 从0开始学Swift笔记整理(五)
  8. 关于TouchEvent里面的touches,targetTouches,changedTouches的解释
  9. 微软SQLHelper.cs类 中文版
  10. POJ 1852
  11. html 实现网址链接
  12. 三、Dotnet Core Code First 创建数据库
  13. ldap数据库--ODSEE--ACI
  14. D01 Elon Mulsk The future we&#39;re building — and boring
  15. LeetCode 884 Uncommon Words from Two Sentences 解题报告
  16. JAVA框架Struts2 数据封装
  17. [转]Idea2016 使用Maven配置简单Web项目(受益比较多的一篇)
  18. 关于MySQL在内网中使用另一台机器访问的问题
  19. java第一天!
  20. Python:基本运算、基本函数(包括复数)、Math模块、NumPy模块

热门文章

  1. 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)
  2. op+3g
  3. 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错
  4. Android SQLite简介
  5. qwtplot3D安装及运行-----终结解决方案
  6. 2.4 Git 基础 - 撤消操作
  7. C#冒泡泡算法
  8. javaweb 学习的好地方
  9. cellForRowAtIndexPath方法不执行的那些坑
  10. TelerikUI_RadGrid_Filter 自定义方法