LeetCode_Spiral Matrix
2024-08-26 06:07:21
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;
}
};
最新文章
- Spring 4 异常处理
- Oracle 在线重定义表分区
- CSS-清除浮动
- PS 切图
- mysql 内存相关文章
- Parametric Curves and Surfaces
- 从0开始学Swift笔记整理(五)
- 关于TouchEvent里面的touches,targetTouches,changedTouches的解释
- 微软SQLHelper.cs类 中文版
- POJ 1852
- html 实现网址链接
- 三、Dotnet Core Code First 创建数据库
- ldap数据库--ODSEE--ACI
- D01 Elon Mulsk The future we&#39;re building — and boring
- LeetCode 884 Uncommon Words from Two Sentences 解题报告
- JAVA框架Struts2 数据封装
- [转]Idea2016 使用Maven配置简单Web项目(受益比较多的一篇)
- 关于MySQL在内网中使用另一台机器访问的问题
- java第一天!
- Python:基本运算、基本函数(包括复数)、Math模块、NumPy模块
热门文章
- 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)
- op+3g
- 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错
- Android SQLite简介
- qwtplot3D安装及运行-----终结解决方案
- 2.4 Git 基础 - 撤消操作
- C#冒泡泡算法
- javaweb 学习的好地方
- cellForRowAtIndexPath方法不执行的那些坑
- TelerikUI_RadGrid_Filter 自定义方法