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

这道题挺简单的,基本上算是一次性写出来的,就是设立一个对应的标志数组,然后按照螺旋的规则来遍历。

代码如下:

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
int height=matrix.size();
if( height==)
return res;
int width=matrix[].size();
vector<vector<int>> flag(height,vector<int>(width,));//用来记录是否走过
int m=;
int n=;
flag[][]=;
res.push_back(matrix[][]);
int step=;
while(step!= height* width)
{
while(n+<width&&flag[m][n+])
{
flag[m][n+]=;
res.push_back(matrix[m][n+]);
n+=;
step++;
}
while(m+<height&&flag[m+][n])
{
flag[m+][n]=;
res.push_back(matrix[m+][n]);
m+=;
step++;
}
while(n->=&&flag[m][n-])
{
flag[m][n-]=;
res.push_back(matrix[m][n-]);
n-=;
step++;
}
while(m->=&&flag[m-][n])
{
flag[m-][n]=;
res.push_back(matrix[m-][n]);
m-=;
step++;
}
}
return res;
}
};

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

For example,
Given n = 3,

You should return the following matrix:

[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

 I写出来了的话,II就更简单了,在I上改一下就行了,代码如下:

class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> flag(n,vector<int>(n,));//用来记录是否走过
if(n==)
return flag;
int height=;
int width=;
int step=;
flag[][]=;
while(step!= n*n)
{
while(width+<n&&flag[height][width+]==)
{
width+=;
step++;
flag[height][width]=step;
}
while(height+<n&&flag[height+][width]==)
{
height+=;
step++;
flag[height][width]=step;
}
while(width->=&&flag[height][width-]==)
{
width-=;
step++;
flag[height][width]=step;
}
while(height->=&&flag[height-][width]==)
{
height-=;
step++;
flag[height][width]=step;
}
}
return flag; }
};

  

Spiral Matrix I&&II的更多相关文章

  1. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  2. Spiral Matrix I & II

    Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...

  3. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  4. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  6. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  7. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  8. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

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

随机推荐

  1. Eclipse的Project Facets属性设置解决项目无故报错

    新检出项目,发现代码无故报错,各种尝试,最终发现是因为  项目右键中的 project Facets 属性中的 java 后面的 version 版本和项目 build path 的 jdk 版本不一 ...

  2. POJ 2391 二分+最大流

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19066   Accepted: 4 ...

  3. 在Linux防火墙上过滤外来的ICMP timestamp

    ICMP timestamp请求响应漏洞 解决方案:  * 在您的防火墙上过滤外来的ICMP timestamp(类型13)报文以及外出的ICMP timestamp回复报文.     具体解决方式就 ...

  4. Microsoft office 2013安装图解

    Microsoft office 2013安装图解... ================ 简介: Microsoft Office 2013(Office 15)是微软的新一代Office办公软件, ...

  5. uboot两阶段代码分析

    1.启动过程特征总结(1)第一阶段为汇编阶段(start.s).第二阶段为C阶段(board.c中的start_armboot 函数)(2)第一阶段在SRAM中.第二阶段在DRAM中(3)第一阶段注重 ...

  6. 【转】Pyhton 单行、多行注释符号使用方法及规范

    转自:Pyhton 单行.多行注释符号使用方法及规范 python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的.python注释也有自己的规范,在文章中会介绍到.注释可以起到一个 ...

  7. matlab向量的排序(自写函数)

    function a_ed = arraysort(a) %冒泡排序法 for i =1:length(a)-1 %进行多少次比较 for j=1+i:length(a) %每次求出最大的数,放在最后 ...

  8. aos.js让页面滚动变得丰富

    (转)<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script ...

  9. css 常用苹方字体

    // 苹方-简 常规体 font-family: PingFangSC-Regular, sans-serif; // 苹方-简 极细体 font-family: PingFangSC-Ultrali ...

  10. Android之极光推送发送自定义消息

    Android端实现主要代码: <span style="font-size:14px;">import java.io.IOException; import jav ...