SpiralMatrix:

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

算法分析:其实就是两重循环遍历,每次都是遍历一圈,然后遍历里面一圈...,需要记录m,n,每次遍历后,m,n都减去2;还要记录坐标x,y;

特例是n=1;m=1;其实m,n为奇数才有这种特例。

  1. public class SpiralMatrix
  2. {
  3. public List<Integer> spiralOrder(int[][] matrix)
  4. {
  5. List<Integer> res = new ArrayList<>();
  6. if(matrix.length == 0 || matrix == null)
  7. {
  8. return res;
  9. }
  10. int m = matrix.length;
  11. int n = matrix[0].length;//得先判断矩阵是否为空,否则数组下标越界
  12. int x = 0, y = 0;
  13. while(m > 0 && n > 0)
  14. {
  15. if(m == 1)
  16. {
  17. for(int i = 0; i < n; i ++)
  18. {
  19. res.add(matrix[x][y++]);
  20. }
  21. break;//别忘了break
  22. }
  23. else if(n == 1)
  24. {
  25. for(int j = 0; j < m; j ++)
  26. {
  27. res.add(matrix[x++][y]);
  28. }
  29. break;
  30. }
  31.  
  32. for(int i = 0; i < n-1; i ++)
  33. {
  34. res.add(matrix[x][y++]);
  35. }
  36. for(int i = 0; i < m-1; i ++)
  37. {
  38. res.add(matrix[x++][y]);
  39. }
  40. for(int i = 0; i < n-1; i ++)
  41. {
  42. res.add(matrix[x][y--]);
  43. }
  44. for(int i = 0; i < m-1; i ++)
  45. {
  46. res.add(matrix[x--][y]);
  47. }
  48. x++;
  49. y++;
  50. m -= 2;
  51. n -= 2;
  52. }
  53. return res;
  54. }
  55. }

SpiralMatrix2:

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

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

算法分析:和上一算法,类似,只不过这是个n*n矩阵,其实可以延伸到m*n矩阵。n*n使清空更简单,特别是n=1的时候。

  1. public class SpiralMatrix2
  2. {
  3. public int[][] generateMatrix(int n)
  4. {
  5. int[][] res = new int[n][n];
  6. if(n == 0)
  7. {
  8. return res;
  9. }
  10. int x = 0, y = 0;
  11. int val = 1;
  12. while(n > 0)
  13. {
  14. if(n == 1)
  15. {
  16. res[x][y] = val;
  17. break;
  18. }
  19. for(int i = 0; i < n-1; i ++)
  20. {
  21. res[x][y++] = val;
  22. val ++;
  23. }
  24. for(int i = 0; i < n-1; i ++)
  25. {
  26. res[x++][y] = val;
  27. val ++;
  28. }
  29. for(int i = 0; i < n-1; i ++)
  30. {
  31. res[x][y--] = val;
  32. val ++;
  33. }
  34. for(int i = 0; i < n-1; i ++)
  35. {
  36. res[x--][y] = val;
  37. val ++;
  38. }
  39. x ++;
  40. y ++;
  41. n -= 2;
  42. }
  43. return res;
  44. }
  45. }

矩阵螺旋遍历Spiral Matrix,Spiral Matrix2的更多相关文章

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

  2. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  3. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  4. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  5. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  7. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  8. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  9. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

随机推荐

  1. 160705、总结:commons-codec.jar中常用方法

    一.Base64编码和解码import org.apache.commons.codec.EncoderException;import org.apache.commons.codec.binary ...

  2. MySQL中的注释(有三种)

    MysQL支持三种注释: .#... (推荐这种,具有通性) ."-- ..." (注意--后面有一个空格) ./*...*/

  3. 17.Recflection_反射

    www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html

  4. query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max

    3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...

  5. 第09章—使用Lombok插件

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  6. Tomcat WEB站点部署

    上线的代码有两种方式, 第一种方式是直接将程序目录放在webapps目录下面 第二种方式是使用开发工具将程序打包成war包,然后上传到webapps目录下面.下面让我们见识一下这种方式 这个网站里面已 ...

  7. 华为大数据项目fusionInsight

    项目简述:基于开源Hadoop2.0架构的集群网络,进行海量数据的分布式计算.由于Hadoop集群规模不断扩大,而搭建一个同等规模的测试集群需要一笔昂贵的开销.目前有100台左右物料,期望预测计算节点 ...

  8. yum速查

    yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包, 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖 ...

  9. directorjs和requirejs和artTemplate模板引擒建立的SPA框架

    分为4块:A : index.html壳子.    加载B  init-config.js,   加载D  header.html模板B : init-config.js 个人信息+路由配置+权限+渲 ...

  10. LightOJ - 1356 Prime Independence (数论+二分图匹配)

    题意:有N个数的集合,其中选出若干个数组成一个子集,要求这个子集中的任意两个数a,b都不能通过a=k*b得到,其中k是一个素数.求这个子集最大的size. 分析:集合中任意两数的关系是二者之间是否之差 ...