[抄题]:

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

Example 1:

  1. Input:
  2. [
  3. [ 1, 2, 3 ],
  4. [ 4, 5, 6 ],
  5. [ 7, 8, 9 ]
  6. ]
  7. Output: [1,2,3,6,9,8,7,4,5]

Example 2:

  1. Input:
  2. [
  3. [1, 2, 3, 4],
  4. [5, 6, 7, 8],
  5. [9,10,11,12]
  6. ]
  7. Output: [1,2,3,4,8,12,11,10,9,5,6,7]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

变量变了之后,随即就要用if控制范围了,不然会越界:

  1. if (rowBegin <= rowEnd && colBegin <= colEnd)

[思维问题]:

感觉表示corner的变量总是变,不好表示。新开四个新变量就行了,反正也不占用空间。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. start/end都是要加进去(包括进去)的数,所以务必要写等号

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

新开几个记录的变量,并不占用空间

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

  1. public class Solution {
  2. public List<Integer> spiralOrder(int[][] matrix) {
  3.  
  4. List<Integer> res = new ArrayList<Integer>();
  5.  
  6. if (matrix.length == 0) {
  7. return res;
  8. }
  9.  
  10. int rowBegin = 0;
  11. int rowEnd = matrix.length-1;
  12. int colBegin = 0;
  13. int colEnd = matrix[0].length - 1;
  14.  
  15. while (rowBegin <= rowEnd && colBegin <= colEnd) {
  16. // Traverse Right
  17. for (int j = colBegin; j <= colEnd; j ++) {
  18. res.add(matrix[rowBegin][j]);
  19. }
  20. rowBegin++;
  21.  
  22. // Traverse Down
  23. for (int j = rowBegin; j <= rowEnd; j ++) {
  24. res.add(matrix[j][colEnd]);
  25. }
  26. colEnd--;
  27.  
  28. if (rowBegin <= rowEnd && colBegin <= colEnd) {
  29. // Traverse Left
  30. for (int j = colEnd; j >= colBegin; j --) {
  31. res.add(matrix[rowEnd][j]);
  32. }
  33. }
  34. rowEnd--;
  35.  
  36. if (colBegin <= colEnd && rowBegin <= rowEnd) {
  37. // Traver Up
  38. for (int j = rowEnd; j >= rowBegin; j --) {
  39. res.add(matrix[j][colBegin]);
  40. }
  41. }
  42. colBegin ++;
  43. }
  44.  
  45. return res;
  46. }
  47. }

54. Spiral Matrix以螺旋顺序输出数组的更多相关文章

  1. LeetCode 54. Spiral Matrix(螺旋矩阵)

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

  2. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

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

  5. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

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

  7. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  8. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  9. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

随机推荐

  1. cookie mapping 原理理解

    深入浅出理解 COOKIE MAPPING Cookie mapping技术 利用javascript跨域访问cookie之广告推广

  2. uniq的坑坑

    很久没有做过文本统计之类的操作了,今天有点任务弄一下,幸亏机智的我列出了全部看了一遍,发现uniq的时候还是有重复的,然后总结了一下 假如我有1.txt这个文本: 10.0.0.1 10.0.0.1 ...

  3. 18.1 volatile的作用

    volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一 ...

  4. C#编程经验-VS Debug

    F11 OneStepDebugF10 ProcessDebugbreakPointDebug(quick location,then use one step debug)

  5. makefile或shell中的一些变量

    总是记不住,作个笔记 $@ 所有目标文件 $< 第一个依赖文件的名称 $? 所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚 $^ 所有的依赖文件,以空格分开,不包含重复的依 ...

  6. tomcat窗口一闪而过

    当点击bin/startup.bat,出现黑窗口一闪而过时,肯定是因为tomcat启动报错了. 错误排查方法 首先检查java环境变量是否设置正确. 其次调试tomcat,需要修改startup.ba ...

  7. spring4.0之二:@Configuration的使用

    从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplic ...

  8. 解决配置Windows Update失败,还原更改问题

    问题描述 由于配置Windows Update失败,还原更改状态下无法正常关机.只能长按电源键关机后进入WinPE环境. 解决步骤 进入WinPE环境->选择Dism++->选择版本-&g ...

  9. Scrapy学习篇(十二)之设置随机IP代理(IPProxy)

    当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...

  10. html/css/js-横向滚动条的实现

    在前端UI设计时,网页的制作很麻烦,深有感悟!碰到太多的不懂,或是第一次见,就要去网上找资料!横向滚动条就是我遇到麻烦中其中的一个,其实也 很简单,只是在几次项目中都用到了这个横向滚动条所以就拿出来说 ...