题目:

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. 基于Redis主从复制读写分离架构的Session共享

    1.搭建主从复制 第一步:将Redis拷贝到虚拟机上的指定文件夹内,此Redis作为主服务 第二步:将Redis拷贝到本机的指定文件夹内,此Redis作为从服务 第三步:修改主服务的配置文件(redi ...

  2. allegro 16.6 空心焊盘的制作

    手机键盘的按键就是空心焊盘,新建一个外径为0.6mm 内径为0.4mm 的空心焊盘 空心焊盘的制作如下: 一.新建一个空心的shape 1 shape -> Cirrular 在坐标处输入 x ...

  3. MVC学习系列——ActionResult扩展

    首先,MVC扩展性非常强. 我从ActionResult扩展入手,因为我们知道微软ActionResult和其子类,有时候并不能满足所有返回值. 比如:我需要返回XML. 因此,现在我扩展XMLRes ...

  4. NET

    NET狂官方面试题-数据库篇答案   题目:http://www.cnblogs.com/dunitian/p/6028838.html 汇总:http://www.cnblogs.com/dunit ...

  5. SpringMVC+redis整合

    在网络上有一个很多人转载的springmvc+redis整合的案例,不过一直不完整,也是被各种人装来转去,现在基本将该框架搭建起来. package com.pudp.bae.base; import ...

  6. 传说中的Markov"不过如此”

    因为看一篇题为 Passive Measurement of Interference in WiFi Network with Application in Misbehavior Detectio ...

  7. 处理一则MySQL Slave环境出现ERROR 1201 (HY000): Could not initialize master info structure的案例

    mysql> start slave; ERROR (HY000): Slave failed to initialize relay log info structure from the r ...

  8. BZOJ1692: [Usaco2007 Dec]队列变换

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 594  Solved: 246[Submit][Sta ...

  9. oracle Execute Immediate 用法

      包含using into用法. Declare        v_sid Integer:=20020101;        v_sql Varchar2(100);        v_resul ...

  10. httpsClient

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...