题目:

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. Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo

    1.新建Dll工程 2.Dll工程全部代码 library SubMain; { Important note about DLL memory management: ShareMem must b ...

  2. js实现复制到剪切板

    // <![CDATA[ function copy_clip(copy) { if (window.clipboardData) { window.clipboardData.setData( ...

  3. Vim自动补全神器:YouCompleteMe

    第一次听说这个插件还是在偶然的情况下看到别人的博客,听说了这个插件的大名.本来打算在实训期间来完成安装的,无奈网实在不给力,也就拖到了回家的时候.在开始准备工作的时候就了解到这个插件不是很容易安装,安 ...

  4. linux 压缩文件 及压缩选项详解

    本文介绍linux下的压缩程序tar.gzip.gunzip.bzip2.bunzip2.compress.uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们对. ...

  5. Java高效读取大文件

    1.概述 本教程将演示如何用Java高效地读取大文件.这篇文章是Baeldung (http://www.baeldung.com/) 上“Java——回归基础”系列教程的一部分. 2.在内存中读取 ...

  6. RCP,TCP,C/S,B/S

    RCP: RICH CLIENT PROGRAM   胖客户端 TCP: THIN CLIENT PROGRAM   瘦客户端 CS: CLIENT SERVER             客户端/服务 ...

  7. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  8. Ligerui Grid组件--学生信息列表

    一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...

  9. IIS搭建本地服务器,花生壳实现外网通过域名访问网站

    配置服务器 作为一个青年,没有实力,做不出标图所示的服务器. 作为一个学生,买不起服务器 作为一个小孩,买不起域名 但别忘了 作为一个平民玩家,只要有耐心 装备迟早会做出来的 (注:感觉有钱与没钱还是 ...

  10. 如何用jmeter对websock和protobuf进行压力测试

    1. 一个websocket插件官网地址 https://github.com/maciejzaleski/JMeter-WebSocketSampler 2. 可以用上述插件,也可以自己扩展,以实现 ...