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的更多相关文章

  1. LeetCode_Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

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

  4. Atitit Data Matrix dm码的原理与特点

    Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...

  5. Android笔记——Matrix

    转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...

  6. 通过Matrix进行二维图形仿射变换

    Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. CCI_chapter 3 Stacks and Queues

    3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, ...

  2. Wireshark "The NPF driver isn’t running…"(可见的驱动本质上是一个系统服务,使用net start 启动)

    前几天重装系统,装上了windows7 RC系统.昨天开始尝试装上了wireshark 这款很强大的网络监视软件,满心欢喜的打开,可是每次打开都会弹出“The NPF driver isn't run ...

  3. 解决MVC项目中,静态html 未找到时候,404的跳转

    using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...

  4. cf443B Kolya and Tandem Repeat

    B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. 为什么新建的管理员账号权限没有Administrator大?

    Administrator是超级管理员,UAC不用确认,跟关了一样. 新建隶属于administrator组的用户,可以关掉UAC. 控制面板>系统和安全>操作中心>更改用户帐户控制 ...

  6. 如何改变Myeclipse编辑区背景色

    编辑窗口右键单击——>Preferences——>General加号——>Editors加号——>点Text Editors字样——>右下窗口选Backgroud col ...

  7. [Err] 1449 - The user specified as a definer ('admin_isbox'@'localhost') does not exist

    晚上加班调用一个远程拷贝的本地Mysql的储存过程,报错:[Err] 1449 - The user specified as a definer ('admin_isbox'@'localhost' ...

  8. 程序员的绘图利器 — Gnuplot

      介绍 Gnuplot is a command-line program that can generate two- and three-dimensional plots. It is fre ...

  9. XML基本知识

    一.xml简介 1.xml(可扩展标记语言),是一种标记语言,类似于html,其作用主要是传输数据,并非显示数据! 2.xml标签没有被预定义需要用户自行定义. 3.xml由w3c组织发布,遵循200 ...

  10. 今天上传公司服务器出现的.net framework版本错误问题

    今天做好一个网站(.net4.0),里面有静态页面也有aspx页面,发布后,满心欢喜的上传到服务器,运行后,静态页没有问题,可是通过导航栏一旦点击进入aspx页面,就会出现错误 ,提示web.conf ...