Given a matrix of m x n elements (m rows, n columns), 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].

解题思路:

螺旋阵列,使用up down left right 四个指针标记上下左右的位置即可,JAVA实现如下:

static public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<Integer>();
if (matrix.length == 0 || matrix[0].length == 0)
return list;
int left = 0, right = matrix[0].length - 1, up = 0, down = matrix.length - 1;
while (left <= right && up <= down) {
for (int i = left; i <= right&&up<=down; i++)
list.add(matrix[up][i]);
up++;
for (int i = up; i <= down&&left<=right; i++)
list.add(matrix[i][right]);
right--;
for (int i = right; i >= left&&up<=down; i--)
list.add(matrix[down][i]);
down--;
for (int i = down; i >= up&&left<=right; i--)
list.add(matrix[i][left]);
left++;
}
return list;
}

Java for LeetCode 054 Spiral Matrix的更多相关文章

  1. Java for LeetCode 059 Spiral Matrix II

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

  2. [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 ...

  3. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  4. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  5. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

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

  7. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  8. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  9. [leetcode]54. Spiral Matrix二维数组螺旋取数

    import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

  2. Teradata(不同date输出要求;表类型)

    1. 需要某种特定形式的date 类型export 到文件中,例如 YYYYMMDD/ YYYY-MM-DD 这时候不一定非要用date 类型,可以转换为varchar 类型! CAST(CAST ( ...

  3. Ubuntu 及衍生版本用户如何安装 SmartGit/HG

    http://www.linuxidc.com/Linux/2014-06/102621.htm Ubuntu 及衍生版本用户如何安装 SmartGit/HG 6.0.0 [日期:2014-06-03 ...

  4. Prim和Kruskal求最小生成树

    Prim: 算法步骤: 1.任意结点开始(不妨设为v1)构造最小生成树: 2.首先把这个结点(出发点)包括进生成树里, 3.然后在那些其一个端点已在生成树里.另一端点还未在生成树里的所有边中找出权最小 ...

  5. mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题

    我们公司的项目使用spring+mybatis组合.所以就必须得使用mybatis-spring了.所以此处就昨日mybatis-spring从1.1升级到1.2所带来的dao层级的编写问题,做了一个 ...

  6. javascript的字符串模板

    在其他语言存在字符串内插(string interpolation)或者叫变量内插(Variable interpolation).ES6中的称为template string. 模板字符串使用反引号 ...

  7. POI读写Excel简述之写入

    二.POI写入Excel文件(以Excel2003版为例,2007版就是根据文件扩展名xlsx将HSSFWorkbook换为XSSFWorkbook,及其Sheet.Row.Cell也相应替换) 1. ...

  8. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  9. iOS视图生命周期与视图控制器关系

    iOS中视图是一个应用的重要组成部分,功能的实现与其息息相关,而视图控制器控制着视图,其重要性在整个应用中不言而喻.   视图生命周期与视图控制器关系   以视图的5种状态为基础,我们来系统的了解一下 ...

  10. Xcode6中添加pch全局引用文件

    前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6添加pch文件方法 1. 右键Supporting File,选择“New File” 2. 选择Other ...