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. shell expr的用法

    root@tcx4440-03:~# var=$var+1root@tcx4440-03:~# echo $var3+1 要想达到预期结果,用下列三种方法: (1)let "var+=1&q ...

  2. 【HDU 5007】Post Robot

    Description DT is a big fan of digital products. He writes posts about technological products almost ...

  3. 洛谷P1363 幻想迷宫

    题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...

  4. SPOJ Pouring Water

    传送门 POUR1 - Pouring water #gcd #recursion Given two vessels, one of which can accommodate a litres o ...

  5. vim YouCompleteMe

    http://www.ithao123.cn/content-1906969.html http://www.it165.net/os/html/201503/12190.html

  6. 【基础语法】a++与++a的区别

    package com.on.learn.e2; /** * @author lj * 自增:a++与++a a++是指本行表达式不使用a自增后的值,++a是指本行开始就已经使用a自增后的值 * */ ...

  7. linux虚拟机系统的复制或克隆后续问题解决!

    前言 加快创建hadoop或spark集群,方法有两种途径:克隆或复制.其实啊,我最近,再返回写下本博文,理清下思路. 比如,你在你的一台电脑里,安装虚拟机.已经搭建好了hadoop或spark集群. ...

  8. POJ1088滑雪(dp+记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 86411   Accepted: 32318 Description ...

  9. ECSHOP手机号码或邮箱用户名都可以登录方法

    ECSHOP手机号码或邮箱用户名都可以登录方法 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-06-30   有不少人都在找支持ECShop用户名.邮箱或手号 ...

  10. Effective Java之避免创建不必要的对象

    Effective Java中有很多值得注意的技巧,今天我刚开始看这本书,看到这一章的时候,我发现自己以前并没有理解什么是不必要的对象,所以拿出来跟大家探讨一下,避免以后犯不必要的错误! 首先书中对不 ...