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. oracle基本语句

    ALTER TABLE SCOTT.TEST RENAME TO TEST1--修改表名 ALTER TABLE SCOTT.TEST RENAME COLUMN NAME TO NAME1 --修改 ...

  2. UVa 437 The Tower of Babylon

    Description   Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of ...

  3. LFI、RFI、PHP封装协议安全问题学习

    本文希望分享一些本地文件包含.远程文件包含.PHP的封装协议(伪协议)中可能包含的漏洞 相关学习资料 http://www.ibm.com/developerworks/cn/java/j-lo-lo ...

  4. (原)java中对象复制、==、equals

    对于基本数据类型而言,即如下八种基本数据类型,int,boolean,char,byte,short,float,double,long. public class test { public sta ...

  5. hihocoder 1196 高斯消元.二

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中,小Hi和小Ho趁着便利店打折,买了一大堆零食.当他们结账后,看到便利店门口还有其他的活动. 店主:买了 ...

  6. html标签(一)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. emmet使用 及 notepadd++ emmet的安装

    emmet的使用的参考文章:http://www.cnblogs.com/sussski/p/3544744.html html:4s.html:4t.html:5或! +.>.^:层次 *.@ ...

  8. java 小记

    1.获取web项目根目录的绝对路径 request.getContextPath()  获取项目名称,如    /BiYeSheJi getServletContext().getRealPath(& ...

  9. 七层负载均衡——HAProxy

    HAProxy入门 HAProxy是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,HAProxy是完全免费的.借助HAProxy可以快速并且可靠的提供基于TCP ...

  10. git小结

    1.创建本地与远程分支 先创建远程分支,再创建本地分支,再将本地分支与远程分支关联git fetch 获取远程分支git checkout remote_branch 或者 git checkout ...