【一天一道LeetCode】#54. Spiral Matrix
一天一道LeetCode系列
(一)题目
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].
(二)解题
相当于顺时针打印矩阵。我想到的方法就是一圈一圈的打印,每一圈的起点分别是(0,0),(1,1)…..,那么退出循环打印的条件是什么呢?
我们可以分析3*3的矩阵有两圈,4*4的矩阵有两圈,5*5的矩阵有三圈,6*6的矩阵有三圈…..
假设m*n的矩阵,起点位置为(ori,ori),那么满足的条件为m>ori*2&&n>ori*2
具体思路见代码注释:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ret;
if(matrix.size() ==0) return ret;//矩阵为空的时候直接返回
int ori=0;//每一圈的起点
int row = matrix.size();
int col = matrix[0].size();
int px=ori,py=ori;
int i , j , x, y;
while(row>ori*2 && col > ori*2)//退出循环的条件
{
bool flag1 = false, flag2 = false, flag3 = false;//整圈打印需要连续,如果发生某一个方向没打印就下面的就不需要判断了
for (i = px; i < col - px; i++)//从左往右
{
ret.push_back(matrix[py][i]);
flag1 = true;
}
px = i-1;//i越界了,应该减1
for (j = py+1; j < row - py && flag1; j++)//从上到下
{
ret.push_back(matrix[j][px]);
flag2 = true;
}
py = j-1;//同上
for (x = px-1; x >= ori&&flag2; x--)//从右往左
{
ret.push_back(matrix[py][x]);
flag3 = true;
}
px = x+1;//同上
for (y = py-1; y > ori &&flag3; y--)//从下到上
{
ret.push_back(matrix[y][px]);
}
px = ++ori;//更新起点x
py = ori;//更新起点y
}
return ret;
}
};
【一天一道LeetCode】#54. Spiral Matrix的更多相关文章
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [leetcode]54. Spiral Matrix螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode: 54. Spiral Matrix(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...
- [leetcode]54. Spiral Matrix二维数组螺旋取数
import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...
随机推荐
- pandas小记:pandas高级功能
http://blog.csdn.net/pipisorry/article/details/53486777 pandas高级功能:面板数据.字符串方法.分类.可视化. 面板数据 {pandas数据 ...
- Django 缓存模块 page_cache 源码阅读
Django cache中比较常用的有 cache_page 这么个 decorators, 下面就根据请求流程,结合源码来说说它是怎么工作的? 版本是django1.8,不同版本可能函数等会变化,逻 ...
- windows上安装nodejs,升级npm,安装webpack指南
安装nodejs https://nodejs.org/en/ 安装webpack和其他一些常用的 npm install -g node-gyp webpack coffee-script 监控 w ...
- UILabel 调整行间距
/* 调整行间距 */ + (void)adjustLineSpacingOfLabel:(UILabel *)label to:(CGFloat)lineSpacing { NSString *te ...
- rbac数据库设计
1 rbac数据库设计 RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制分享牛原创,分享牛系列,分享牛.rbac 用户角色权限资源表如 ...
- Swift:一个简单的货币转换器App在iOS10中的分析和完善
这本不算是一个完整的货币转换App,只不过是一个小巧的学习性质的程序.该App覆盖了如下几个知识点: 多国语言的支持 通过网络Api接口读取数据 最后我们来修复一个原来代码中的一个小错误作为完美的收尾 ...
- Windows运行GitStats
Windows运行GitStats(金庆的专栏)GitStats - git history statistics generatorhttp://gitstats.sourceforge.net/G ...
- (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)
接下来,我们再讲一下图文消息: 如图: 大家可以先从开发者文档中了解一下图文消息的一些参数: 如上图,用户回复4时,ipastor返回了几条图文消息,上图中属于多图文消息,当然还有单图文消息,图文消息 ...
- 我的第一个RootKit,支持XP、Vista、Win7、Win8 RTM 32位
只有写过一个BootKit,才能比较深刻的理解其整个过程与机制,也能加深对Windows系统引导各个过程的熟悉和理解. 我写的这个bootkit,暂时还没想到一个比较好的名字,它 1. 支持xp到w ...
- JBOSS EAP 6 系列一 新特性
在项目中,采用的架构是Springmvc+spring+EJB+Jpa等架构,当然服务器是Jboss,本次Jboss我们采用的是JBossEap6.2,Jboss7的新特性与Jboss4.5的大的改变 ...