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. linux ftp 上传与下载命令解析

    month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...

  2. 170302、 Apache 使用localhost(127.0.0.1)可以访问,使用本机局域网IP(192.168.2.*)不能访问

    对于此问题的解决办法,打开apache安装路径中的http.conf文件, 找打以下内容 #   onlineoffline tag - don't remove        Order Deny, ...

  3. Centos7 下安装mysql数据库

    centos7系统,安装mysql发现已经默认的是mariadb. 只能安装mariadb,mariadb是mysql一个分支,对mysql完全支持 1 安装 yum -y install maria ...

  4. MongoDB-6: MongoDB索引

    一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...

  5. Andrew Ng机器学习编程作业:Anomaly Detection and Recommender Systems

    作业文件 machine-learning-ex8 在本次练习,第一节我们将实现异常检测算法,并把它应用到检测网络故障服务器上.在第二部分,我们将使用协同过滤来构建电影推荐系统. 1. 异常检测 在这 ...

  6. golang SQLite3性能测试

    SQLite是个小型的数据库,很简洁,即支持文件也支持内存,比较适合小型的独立项目,在没有网络的时候做一些复杂的关系数据存储和运算. 为了考察性能做10M(1000万)条记录的测试,测试机4CPU.8 ...

  7. Web框架简介

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. React:快速上手(5)——掌握Redux(2)

    React:快速上手(5)——掌握Redux(2) 本文部分内容参考阮一峰的Redux教程. React-Redux原理 React-Redux运行机制 我觉得这张图清楚地描述React-Redux的 ...

  9. python约束 与MD5加密写法

    python 中约束写法有两种 1 常用的通过继承关系主动抛出异常  2 通过抽象类+抽象方法 1 常用的通过继承关系主动抛出异常写法 在本send方法中报错不会抛出异常, class BaseMes ...

  10. hadoop单击模式环境搭建

    一 安装jdk 下载相应版本的jdk安装到相应目录,我的安装目录是/usr/lib/jdk1.8.0_40 下载完成后,在/etc/profile中设置一下环境变量,在文件最后追加如下内容 expor ...