LeetCode_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].
注意几点: 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;
}
};
LeetCode_Spiral Matrix的更多相关文章
- LeetCode_Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- Some General concepts in ISO C
[ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...
- V$LATCH_PARENT和V$LATCH_CHILDREN
V$LATCH_PARENT contains statistics about parent latches. The columns of V$LATCH_PARENT are identical ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- c# splitter控件使用简介
摘自:http://blog.itpub.net/26221264/viewspace-735903 1.先在窗体上放置部分一的控件,这里是TreeView控件,然后把它的 Dock 属性设置为 Le ...
- BuguMongo是一个MongoDB Java开发框架,集成了DAO、Query、Lucene、GridFS等功能
http://code.google.com/p/bugumongo/ 简介 BuguMongo是一个MongoDB Java开发框架,它的主要功能包括: 基于注解的对象-文档映射(Object-Do ...
- Oracle442个应用场景------------基础应用场景
/////////////////基础知识////////////////// 应用场景178:最简单的select语句 SELECT * FROM Employees; 应用场景179:指定要查询的 ...
- 用消息在Win32控制台程序多线程间进行通讯
#include <stdio.h> #include <windows.h> //#include <iostream> //#include <pro ...
- Samba-ADS/WINBIND
Samba Security = ADShttp://www.justlinux.com/forum/showt...hreadid=118288 Winbindhttp://www.justlinu ...
- 静态方法中不能new内部类的实体对象
原因如下: 1.内部类可以访问外部类的成员变量 2.对象创建完成后对象的成员变量才会被分配空间 3.main的静态方法执行时可以不存在外部类,不创建实体对象 4.内部类能访问成员变量意味着一定存在外部 ...
- css-盒模型,浮动,定位之间的关系
网站布局属性:盒模型:调整元素间距float浮动:竖排的块级元素改成横排position定位:重叠元素,精确控制元素位置 能用盒模型,不用float,能用浮动,不用定位