54. Spiral Matrix以螺旋顺序输出数组
[抄题]:
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
- Input:
- [
- [ 1, 2, 3 ],
- [ 4, 5, 6 ],
- [ 7, 8, 9 ]
- ]
- Output: [1,2,3,6,9,8,7,4,5]
Example 2:
- Input:
- [
- [1, 2, 3, 4],
- [5, 6, 7, 8],
- [9,10,11,12]
- ]
- Output: [1,2,3,4,8,12,11,10,9,5,6,7]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
变量变了之后,随即就要用if控制范围了,不然会越界:
- if (rowBegin <= rowEnd && colBegin <= colEnd)
[思维问题]:
感觉表示corner的变量总是变,不好表示。新开四个新变量就行了,反正也不占用空间。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- start/end都是要加进去(包括进去)的数,所以务必要写等号
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
新开几个记录的变量,并不占用空间
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
- public class Solution {
- public List<Integer> spiralOrder(int[][] matrix) {
- List<Integer> res = new ArrayList<Integer>();
- if (matrix.length == 0) {
- return res;
- }
- int rowBegin = 0;
- int rowEnd = matrix.length-1;
- int colBegin = 0;
- int colEnd = matrix[0].length - 1;
- while (rowBegin <= rowEnd && colBegin <= colEnd) {
- // Traverse Right
- for (int j = colBegin; j <= colEnd; j ++) {
- res.add(matrix[rowBegin][j]);
- }
- rowBegin++;
- // Traverse Down
- for (int j = rowBegin; j <= rowEnd; j ++) {
- res.add(matrix[j][colEnd]);
- }
- colEnd--;
- if (rowBegin <= rowEnd && colBegin <= colEnd) {
- // Traverse Left
- for (int j = colEnd; j >= colBegin; j --) {
- res.add(matrix[rowEnd][j]);
- }
- }
- rowEnd--;
- if (colBegin <= colEnd && rowBegin <= rowEnd) {
- // Traver Up
- for (int j = rowEnd; j >= rowBegin; j --) {
- res.add(matrix[j][colBegin]);
- }
- }
- colBegin ++;
- }
- return res;
- }
- }
54. Spiral Matrix以螺旋顺序输出数组的更多相关文章
- 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 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- [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 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 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 ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- cookie mapping 原理理解
深入浅出理解 COOKIE MAPPING Cookie mapping技术 利用javascript跨域访问cookie之广告推广
- uniq的坑坑
很久没有做过文本统计之类的操作了,今天有点任务弄一下,幸亏机智的我列出了全部看了一遍,发现uniq的时候还是有重复的,然后总结了一下 假如我有1.txt这个文本: 10.0.0.1 10.0.0.1 ...
- 18.1 volatile的作用
volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一 ...
- C#编程经验-VS Debug
F11 OneStepDebugF10 ProcessDebugbreakPointDebug(quick location,then use one step debug)
- makefile或shell中的一些变量
总是记不住,作个笔记 $@ 所有目标文件 $< 第一个依赖文件的名称 $? 所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚 $^ 所有的依赖文件,以空格分开,不包含重复的依 ...
- tomcat窗口一闪而过
当点击bin/startup.bat,出现黑窗口一闪而过时,肯定是因为tomcat启动报错了. 错误排查方法 首先检查java环境变量是否设置正确. 其次调试tomcat,需要修改startup.ba ...
- spring4.0之二:@Configuration的使用
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplic ...
- 解决配置Windows Update失败,还原更改问题
问题描述 由于配置Windows Update失败,还原更改状态下无法正常关机.只能长按电源键关机后进入WinPE环境. 解决步骤 进入WinPE环境->选择Dism++->选择版本-&g ...
- Scrapy学习篇(十二)之设置随机IP代理(IPProxy)
当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...
- html/css/js-横向滚动条的实现
在前端UI设计时,网页的制作很麻烦,深有感悟!碰到太多的不懂,或是第一次见,就要去网上找资料!横向滚动条就是我遇到麻烦中其中的一个,其实也 很简单,只是在几次项目中都用到了这个横向滚动条所以就拿出来说 ...