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. [ 1, 2, 3 ],
  3. [ 4, 5, 6 ],
  4. [ 7, 8, 9 ]
  5. ]

You should return [1,2,3,6,9,8,7,4,5].

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

代码如下:

  1. class Solution {
  2. public:
  3. vector<int> spiralOrder(vector<vector<int>>& matrix) {
  4. vector<int> res;
  5. int height=matrix.size();
  6. if( height==)
  7. return res;
  8. int width=matrix[].size();
  9. vector<vector<int>> flag(height,vector<int>(width,));//用来记录是否走过
  10. int m=;
  11. int n=;
  12. flag[][]=;
  13. res.push_back(matrix[][]);
  14. int step=;
  15. while(step!= height* width)
  16. {
  17. while(n+<width&&flag[m][n+])
  18. {
  19. flag[m][n+]=;
  20. res.push_back(matrix[m][n+]);
  21. n+=;
  22. step++;
  23. }
  24. while(m+<height&&flag[m+][n])
  25. {
  26. flag[m+][n]=;
  27. res.push_back(matrix[m+][n]);
  28. m+=;
  29. step++;
  30. }
  31. while(n->=&&flag[m][n-])
  32. {
  33. flag[m][n-]=;
  34. res.push_back(matrix[m][n-]);
  35. n-=;
  36. step++;
  37. }
  38. while(m->=&&flag[m-][n])
  39. {
  40. flag[m-][n]=;
  41. res.push_back(matrix[m-][n]);
  42. m-=;
  43. step++;
  44. }
  45. }
  46. return res;
  47. }
  48. };

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. [ 1, 2, 3 ],
  3. [ 8, 9, 4 ],
  4. [ 7, 6, 5 ]
  5. ]

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

  1. class Solution {
  2. public:
  3. vector<vector<int>> generateMatrix(int n) {
  4. vector<vector<int>> flag(n,vector<int>(n,));//用来记录是否走过
  5. if(n==)
  6. return flag;
  7. int height=;
  8. int width=;
  9. int step=;
  10. flag[][]=;
  11. while(step!= n*n)
  12. {
  13. while(width+<n&&flag[height][width+]==)
  14. {
  15. width+=;
  16. step++;
  17. flag[height][width]=step;
  18. }
  19. while(height+<n&&flag[height+][width]==)
  20. {
  21. height+=;
  22. step++;
  23. flag[height][width]=step;
  24. }
  25. while(width->=&&flag[height][width-]==)
  26. {
  27. width-=;
  28. step++;
  29. flag[height][width]=step;
  30. }
  31. while(height->=&&flag[height-][width]==)
  32. {
  33. height-=;
  34. step++;
  35. flag[height][width]=step;
  36. }
  37. }
  38. return flag;
  39.  
  40. }
  41. };

  

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. mysql语句及执行计划

    数据库链接: mysql -uroot -p <!--数据库连接-->mysql -h10.0.0.100 -uuser -passwordshow databases <!--查看 ...

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

  3. android内核源码下载和编译

    1.下载编译 新建kernel目录 ~/srcAndroid/src4.4.4_r1/kernel目录下,输入命令: seven@ThinkPad:~/srcAndroid/src4.4.4_r1/k ...

  4. 删除空格-sed

    如下,我需要提取出‘wan’这个字符串.可以发现在‘wan’的前后是有空格,需要将其删除. # lxc list # lxc list | grep lxdbr0 | awk -F "|&q ...

  5. ReentrantLock和synchronized区别和联系?

    相同:ReentrantLock提供了synchronized类似的功能和内存语义,都是可重入锁. 不同: 1.ReentrantLock功能性方面更全面,比如时间锁等候,可中断锁等候,锁投票等,因此 ...

  6. 【题解】Huge Mods UVa 10692 欧拉定理

    题意:计算a1^( a2^( a3^( a4^( a5^(...) ) ) ) ) % m的值,输入a数组和m,不保证m是质数,不保证互质 裸的欧拉定理题目,考的就一个公式 a^b = a^( b % ...

  7. Moodle插件开发——Blocks(版块)

    前提: 1)     基于Moodle3.0,要求Moodle版本高于2.0 2)     PHP编程基础:语言的了解和开发工具使用 有经验的开发人员和那些只是想程序员的参考文本应参阅附录A. 1.  ...

  8. 2017 济南综合班 Day 4

    T1 外星人 二维前缀和 #include<cstdio> #define N 1001 using namespace std; bool v[N][N]; int sum[N][N]; ...

  9. 【NOIP】提高组2015 斗地主

    [题意]按照斗地主出牌规则,给定手牌求出完的最少步数. [算法]模拟+搜索 [题解] 可以发现除了顺子,其它的出牌规则都和点数无关,只与同点数的牌数有关. 所以可以先暴力枚举要出哪些顺子,然后每一个出 ...

  10. 【Atcoder】CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning

    [题意]给定只含小写字母的字符串,要求分割成若干段使段内字母重组顺序后能得到回文串,求最少分割段数.n<=2*10^5 [算法]DP [题解]关键在于快速判断一个字符子串是否合法,容易发现合法仅 ...