54. Spiral Matrix以螺旋顺序输出数组

时间:2022-09-17 01:07:06

[抄题]:

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的变量总是变,不好表示。新开四个新变量就行了,反正也不占用空间。

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

[一句话思路]:

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

[画图]:

54. Spiral Matrix以螺旋顺序输出数组

[一刷]:

  1. 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以螺旋顺序输出数组的更多相关文章

  1. LeetCode 54&period; 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&colon;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&period; Spiral Matrix 、59&period; Spiral Matrix II

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

  4. &lbrack;LeetCode&rsqb; 59&period; 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&period; Spiral Matrix

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

  6. Leetcode 54&period; Spiral Matrix &amp&semi; 59&period; Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  7. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;54&colon; Spiral Matrix

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

  8. leetCode 54&period;Spiral Matrix&lpar;螺旋矩阵&rpar; 解题思路和方法

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

  9. &lbrack;LeetCode&rsqb; Spiral Matrix II 螺旋矩阵之二

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

随机推荐

  1. rabbitmq之消息重入队列

    说起消息重入队列还得从队列注册消费者说起,客户端在向队列注册消费者之后,创建的channel也会被主队列进程monitor,当channel挂掉后,主队列进程(rabbit_amqqueue_proc ...

  2. hdu5713 K个联通块&lbrack;2016百度之星复赛B题&rsqb;

    dp 代码 #include<cstdio> ; ; int n,m,k,cnt[N]; ]; ][],i,j,l,a,b; int check(int x,int y) { int i; ...

  3. &lbrack;转载&rsqb;Docker的安装配置及使用详解

    简介    官网:http://www.docker.com/,点击get started进入下载,目前三个系统的docker容器都有,Windows版需要win10系统,我的是win7系统一开始用的 ...

  4. bootstrap-轮播图

    <!-- 1.写一个父级,class为carousel slide:添加滑动的效果 data-interval 图片轮播间隔时间,单位ms data-ride="carousel&qu ...

  5. JS传中文到后台需要的处理

    前台JS使用encodeURI函数对中文进行编码. 后台Java使用URIDecoder.decode(str,UTF_8)函数对中文进行解码,之后获得中文原文.

  6. js 中对象属性特性的描述

    如何自定义属性的特性? 用对象.属性的特性和自定义的属性的特性有什么区别? 它的四大特性 writable   enumerable   configable   有什么区别? 先预习一个用对象.属性 ...

  7. CSS3 3D转换

    CSS3允许你使用3D转换来对元素进行格式化. 3D转换方法: rotateX() rotateY() 浏览器支持 属性 浏览器支持 transform           IE10和Firefox支 ...

  8. 简单cpu处理器

    在135例中有一个简单处理器的程序,稍作修改成自己的风格 //date :2013/8/22 //designer :pengxiaoen //function get a mpc with veri ...

  9. &lbrack;FMX&rsqb;获取控件样式中的指定项目以便进行调节

    [FMX]获取控件样式中的指定项目以便进行调节 2017-03-26 • C++ Builder.Delphi.教程 • 暂无评论 • swish •浏览 650 次 FMX 的样式丰富了我们的设计, ...

  10. html&plus;js&plus;css&plus;接口交互&plus;echarts实例一枚

    1. 解决了echarts的展现 2. 解决了echarts全屏幕展现(width:100%;height:100%;) 3. 解决了向接口取数据问题 <!DOCTYPE html> &l ...