Spiral Matrix I&&II
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的更多相关文章
- 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 ...
- 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 ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 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 ...
- Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- 【leetcode】59.Spiral Matrix II
Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...
- 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 ...
随机推荐
- Codeforces Round #401 (Div. 2) A B C 水 贪心 dp
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- AIM Tech Round (Div. 2) A
A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- HDU 5586 (dp 思想)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submis ...
- bzoj1177 [Apio2009]Oil 二维前缀最大值,和
[Apio2009]Oil Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2300 Solved: 932[Submit][Status][Disc ...
- Ajax请求回调函数没有被调用
$.ajax({ type:"post", url:"http://172.16.41.91:8080/FcsServletSSM/users ...
- JAVA JDBC(存储过程和事务管理)
1.什么是存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程 ...
- 【BZOJ1926】【SDOI2010】粟粟的书架 [主席树]
粟粟的书架 Time Limit: 30 Sec Memory Limit: 552 MB[Submit][Status][Discuss] Description 幸福幼儿园 B29 班的粟粟是一 ...
- MSSQL 基础知识001
数据库概述: DBMS(Database Management System,数据库管理系统)和数据库. 平时谈到“数据库”可能有两种含义:MSSQLServer.Oracle等某种DBMS:存放一堆 ...
- poj 1797
2013-09-08 09:48 最大生成树,输出生成树中最短的边儿即可 或者对边儿排序,二份答案+BFS判断是否1连通N 时间复杂度都是O(NlogN)的 附最大生成树pascal代码 //By B ...
- B题 hdu 1407 测试你是否和LTC水平一样高
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1407 测试你是否和LTC水平一样高 Time Limit: 2000/1000 MS (Java/Ot ...