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. D-query SPOJ - DQUERY(莫队)统计不同数的数量

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  2. distcc配置

    原理图: OS: ubuntu Server 12.04 1.安装 apt-get install distcc 2.配置 将文件/etc/default/distcc修改为如下格式 STARTDIS ...

  3. sshSSH Secure Shell Client root用户无法登录解决办法

    最近使用这个工具,普通用户可以登录root用户不可以登录.将vi /etc/ssh/sshd_config按照下述配置解决问题 修改sshd配置文件:vi /etc/ssh/sshd_config P ...

  4. HashMap详谈以及实现原理

    (一).HashMap 基于哈希表的 Map 接口的实现 允许使用 null 值和 null 键 HashMap不是线程安全,想要线程安全,Collections类的静态方法synchronizedM ...

  5. MSSQL,MySQL 语法区别

    1 mysql支持enum,和set类型,sql server不支持 2 mysql不支持nchar,nvarchar,ntext类型 3 mysql的递增语句是AUTO_INCREMENT,而mss ...

  6. Mysql优化小记1

    在项目开发中,需要写个windows服务从sqlserver复制数据到mysql(5.6.13 Win64(x86_64)),然后对这些数据进行计算分析.每15分钟复制一次,每次复制大概200条数据, ...

  7. 51Nod 1228 序列求和

    T(n) = n^k,S(n) = T(1) + T(2) + ...... T(n).给出n和k,求S(n).   例如k = 2,n = 5,S(n) = 1^2 + 2^2 + 3^2 + 4^ ...

  8. bzoj 2956: 模积和 ——数论

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  9. jQuery取值的一些奇奇怪怪的操作

    语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var ch ...

  10. LCD实验学习笔记(六):存储控制器

    s3c2440可使用地址空间为1GB(0x00000000到0x40000000). 1G空间分为8个BANK,每个BANK为128MB. 设27条地址线,和8个片选引脚(nGCS0-nGCS7). ...