题目:

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

For example,
Given the following matrix:

[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]

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

链接:  http://leetcode.com/problems/spiral-matrix/

题解:

转圈打印矩阵,需要设置left, right, top和bot四个变量来控制, 注意每次增加行/列前要判断list所含元素是否小于矩阵的总元素数目。需要再想办法简化一下。

Time Complexity - O(mn), Space Complexity - O(1)

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix == null || matrix.length == 0)
return res;
int rowNum = matrix.length, colNum = matrix[0].length;
int left = 0, right = colNum - 1, top = 0, bot = rowNum - 1; while(res.size() < rowNum * colNum) {
for(int col = left; col <= right; col++)
res.add(matrix[top][col]);
top++;
if(res.size() < rowNum * colNum) {
for(int row = top; row <= bot; row++)
res.add(matrix[row][right]);
right--;
}
if(res.size() < rowNum * colNum) {
for(int col = right; col >= left; col--)
res.add(matrix[bot][col]);
bot--;
}
if(res.size() < rowNum * colNum) {
for(int row = bot; row >= top; row--)
res.add(matrix[row][left]);
left++;
}
} return res;
}
}

二刷:

跟一刷一样,设置上下左右四边界,然后编写就可以了。

Java:

Time Complexity - O(mn), Space Complexity - O(1)

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0) {
return res;
}
int rowNum = matrix.length, colNum = matrix[0].length;
int top = 0, bot = matrix.length - 1, left = 0, right = colNum - 1;
int elementsLeft = rowNum * colNum;
while (elementsLeft >= 0) {
if (elementsLeft >= 0) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
elementsLeft--;
}
top++;
}
if (elementsLeft >= 0) {
for (int i = top; i <= bot; i++) {
res.add(matrix[i][right]);
elementsLeft--;
}
right--;
}
if (elementsLeft >= 0) {
for (int i = right; i >= left; i--) {
res.add(matrix[bot][i]);
elementsLeft--;
}
bot--;
}
if (elementsLeft >= 0) {
for (int i = bot; i >= top; i--) {
res.add(matrix[i][left]);
elementsLeft--;
}
left++;
}
}
return res;
}
}

三刷:

条件是total > 0就可以了,不需要">="。

Java:

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0) return res;
int rowNum = matrix.length, colNum = matrix[0].length;
int left = 0, top = 0, right = colNum - 1, bot = rowNum - 1;
int total = rowNum * colNum;
while (total > 0) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
total--;
}
top++; if (total > 0) {
for (int i = top; i <= bot; i++) {
res.add(matrix[i][right]);
total--;
}
right--;
} if (total > 0) {
for (int i = right; i >= left; i--) {
res.add(matrix[bot][i]);
total--;
}
bot--;
} if (total > 0) {
for (int i = bot; i >= top; i--) {
res.add(matrix[i][left]);
total--;
}
left++;
}
}
return res;
}
}

Reference:

https://leetcode.com/discuss/12228/super-simple-and-easy-to-understand-solution

https://leetcode.com/discuss/62196/ac-python-32ms-solution

https://leetcode.com/discuss/46523/1-liner-in-python

54. Spiral Matrix的更多相关文章

  1. LeetCode - 54. Spiral Matrix

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

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

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

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

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

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

  5. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  6. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

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

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

  8. LeetCode OJ 54. Spiral Matrix

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

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

随机推荐

  1. 【面试虐菜】—— Jboss调优

    吐血整理了以前Jboss以及JVM在生产环境下的调优参数,各种不同的案例,都是来自网友杜撰.整合后,希望对广大使用jboss作为生产应用服务器的朋友有所帮助. JBOSS参数调优 配置deploy/j ...

  2. PowerPoint Office Mix 插件

    一个内嵌在PowerPoint里的一个教学工具,可以以PPT为核心录制视频. 点下面链接了解并安装 https://mix.office.com/ 首先这货是免费,当然是基于PowerPoint的基础 ...

  3. ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

    OS: centos 6.3DB: 5.5.14 测试创建yoon测试表,没有主键,没有索引,基础数据内容如下: mysql> select * from yoon;+----+-------- ...

  4. winrar 压缩文件方法

    问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...

  5. php服务器探针

    <?php /* ---------------------------------------------------- */ /* 程序名称: PHP探针-Yahei /* 程序功能: 探测 ...

  6. .NET基础:C#静态构造函数、静态方法、静态属性

    用一个题目带大家走进静态函数,先看题目 class Program    {        public static int Count = 0;        static Program()   ...

  7. matlab实现高斯消去法、LU分解

    朴素高斯消去法: function x = GauElim(n, A, b) if nargin < 2 for i = 1 : 1 : n for j = 1 : 1 : n A(i, j) ...

  8. 什么是Hadoop,怎样学习Hadoop

    Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上:而且它 ...

  9. Core身份认证

    Core中实现一个基础的身份认证 注:本文提到的代码示例下载地址> How to achieve a basic authorization in ASP.NET Core 如何在ASP.NET ...

  10. FACL的使用

    ACL的使用 ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一 ...