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 ...
随机推荐
- mysql语句及执行计划
数据库链接: mysql -uroot -p <!--数据库连接-->mysql -h10.0.0.100 -uuser -passwordshow databases <!--查看 ...
- LOJ 模拟赛
1.LOJ 507 接竹竿 link dp[i]表示前i个的最大分数,所以dp[i]=max(dp[i-1],dp[j-1]+sum[i]-sum[j-1]) (color i ==color j ...
- android内核源码下载和编译
1.下载编译 新建kernel目录 ~/srcAndroid/src4.4.4_r1/kernel目录下,输入命令: seven@ThinkPad:~/srcAndroid/src4.4.4_r1/k ...
- 删除空格-sed
如下,我需要提取出‘wan’这个字符串.可以发现在‘wan’的前后是有空格,需要将其删除. # lxc list # lxc list | grep lxdbr0 | awk -F "|&q ...
- ReentrantLock和synchronized区别和联系?
相同:ReentrantLock提供了synchronized类似的功能和内存语义,都是可重入锁. 不同: 1.ReentrantLock功能性方面更全面,比如时间锁等候,可中断锁等候,锁投票等,因此 ...
- 【题解】Huge Mods UVa 10692 欧拉定理
题意:计算a1^( a2^( a3^( a4^( a5^(...) ) ) ) ) % m的值,输入a数组和m,不保证m是质数,不保证互质 裸的欧拉定理题目,考的就一个公式 a^b = a^( b % ...
- Moodle插件开发——Blocks(版块)
前提: 1) 基于Moodle3.0,要求Moodle版本高于2.0 2) PHP编程基础:语言的了解和开发工具使用 有经验的开发人员和那些只是想程序员的参考文本应参阅附录A. 1. ...
- 2017 济南综合班 Day 4
T1 外星人 二维前缀和 #include<cstdio> #define N 1001 using namespace std; bool v[N][N]; int sum[N][N]; ...
- 【NOIP】提高组2015 斗地主
[题意]按照斗地主出牌规则,给定手牌求出完的最少步数. [算法]模拟+搜索 [题解] 可以发现除了顺子,其它的出牌规则都和点数无关,只与同点数的牌数有关. 所以可以先暴力枚举要出哪些顺子,然后每一个出 ...
- 【Atcoder】CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning
[题意]给定只含小写字母的字符串,要求分割成若干段使段内字母重组顺序后能得到回文串,求最少分割段数.n<=2*10^5 [算法]DP [题解]关键在于快速判断一个字符子串是否合法,容易发现合法仅 ...