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

原题链接:https://oj.leetcode.com/problems/spiral-matrix/

向右螺旋式遍历。

public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix.length == 0)
return null;
List<Integer> list = new ArrayList<Integer>();
int l = 0, r = matrix[0].length - 1;
int u = 0, d = matrix.length - 1;
while (l <= r && u <= d) {
for (int i = l; i <= r; i++)
list.add(matrix[u][i]);
u++;
if (u > d)
continue;
for (int i = u; i <= d; i++)
list.add(matrix[i][r]);
r--;
if (l > r)
continue;
for (int i = r; i >= l; i--)
list.add(matrix[d][i]);
d--;
if (u > d)
continue;
for (int i = d; i >= u; i--)
list.add(matrix[i][l]);
l++;
}
return list;
}
}

LeetCode——Spiral Matrix的更多相关文章

  1. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  3. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  5. [LeetCode]Spiral Matrix 54

    54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...

  6. LeetCode: Spiral Matrix 解题报告

    Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...

  7. [Leetcode] spiral matrix ii 螺旋矩阵

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

  8. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  9. [leetcode]Spiral Matrix @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. 对比Windows 8模拟器(Simulator)和Windows Phone仿真器(Emulator)

    原文:对比Windows 8模拟器(Simulator)和Windows Phone仿真器(Emulator) 从事移动应用开发,经常会用到模拟器(Simulator)和仿真器(Emulator),本 ...

  2. cocos2dX 它CCScene创建原则和切换模式

    今天, 让我们来看看现场CCScene创建原则和切换模式, 首先, 个什么样子: 我们先来看看效果: 啥也没有: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZX ...

  3. Bug记录:微博的Java SDK返回经纬度错误

    现象:美国的坐标点可能会定位到西藏地区-后发现原来负经度经解析后,均变成正的! 源码: private void getGeoInfo(String geo) { StringBuffer value ...

  4. JAVA设计模式(08):结构化-飞锤(Flyweight)

    当前咱们国家正在大力倡导构建和谐社会,当中一个非常重要的组成部分就是建设资源节约型社会,"浪费可耻,节俭光荣". 在软件系统中,有时候也会存在资源浪费的情况,比如在计算机内存中存储 ...

  5. Leetcode:maximum_depth_of_binary_tree题解

    一.     题目 给定一个二叉树,求它的最大深度.最大深度是沿从根节点,到叶节点最长的路径. 二.     分析 (做到这里发现接连几道题都是用递归,可能就是由于自己时挑的简单的做的吧.) 找出最深 ...

  6. 【Nginx】如何应对HTTP组态

    相同的配置项可以在相同的时间内发生的多个块.例如HTTP片.server片.location片.阻断取之于在配置项值. 处理HTTP配置项分下面4个步骤: 创建数据结构用于存储配置项相应的參数 设定配 ...

  7. 经验36--C#无名(大事,物...)

    有时候,方便代码,它会使用匿名的东西. 1.匿名事件 args.CookieGot += (s, e) =>                 {                     this ...

  8. 【MySQL案件】ERROR 1665 (HY000)

    转载请注明: http://blog.csdn.net/jason_asia/article/details/36240815 1.1.1. ERROR 1665 (HY000) [环境的叙述性说明] ...

  9. cocos2d-x3.0 windows 环境配置

    cocos2d-x3.0 windows 环境配置 参考Oo泡泡糖oO的CSDN博文 :http://blog.csdn.net/u010296979/article/details/24273393 ...

  10. KMP该算法解释(最长公共子)

    一个:介绍KMP算法之前,首先解释一下BF算法 (1)BF算法(传统的匹配算法,是最简单的算法) BF算法是一种常见的模式匹配算法,BF该算法的思想是目标字符串S模式串的第一个字符P的第一个字符,以匹 ...