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 ...
随机推荐
- matlab特殊符号表
Character Sequence Symbol Character Sequence Symbol Character Sequence Symbol \alpha α \upsilon υ \s ...
- HDU 5650 异或
so easy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Codeforces Round #343 (Div. 2) B
B. Far Relative’s Problem time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- iframe的使用及操作
一.iframe的使用方法: 在一个页面中加入iframe代码,例如: <div class="myiframe"> <iframe src="test ...
- [LeetCode] 18. 4Sum ☆☆
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- python读文件和写入文件复习
with open("name.txt",'r') as read_file: for name in read_file: list_name = (name.split(',' ...
- codechef September Challenge 2017 Fill The Matrix
这道题我们发现0就代表相同1代表少1或者大1 那么我们根据题目连边 如果存在1(边权只为或0)个数为奇数的环就是无解 #include<cstdio> #include<cstrin ...
- [Unity]游戏Inside中的Chromatic Aberration效果学习
Chromatic Aberration效果指的是模拟摄像机的拍摄瑕疵导致rgb三个通道的颜色发生了偏移,如 传统的Chromatic Aberration实现往往是基于一个后处理,将rgb采样的坐标 ...
- JS 本地属性与继承属性
判断是否拥有某种属性 1.in 运算符 var obj = {name:'jack'}; alert('name' in obj); // --> true alert('toString' i ...
- recycleView实现item点击更改该item颜色,其它item颜色变回
项目中需要横向滚动效果,按照以前的思路,我会写一个ScrollView,里边加一个LinearLayout,在代码中动态加入控件,然后动态删除或者改变颜色,现在android有了新控件Recycler ...