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. 站点系统压力測试Jmeter+Badboy

    近期项目须要压力測试,因此搜了几款试用,首选的是LoadRunner这款大名鼎鼎的測试软件: LoadRunner11 下载请猛戳这里 传送门LoadRunner破解文件 下载请猛戳这里 传送门Loa ...

  2. c++日历v1.12版

    ////////////////////////////新增信息修改功能,未完善. #include<iostream> #include <string> #include& ...

  3. TCP与UDP的侵略性

    HTTP必须执行在TCP上吗?SSL必须执行在TCP上吗?...实际上HTTP并没有规定一定要执行在TCP上,甚至FTP也不一定要执行在TCP上!HTTP或者FTP仅仅是说底层信道要保证数据的按序传输 ...

  4. 浅析MVC和说媒的过程

    什么是MVC? MVC 全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面 显 ...

  5. Python 获取Twitter用户与Friends和Followers的关系(eg, 交集,差集)

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-30 @author: guaguastd @name: f ...

  6. SQL开发中容易忽视的一些小地方(四)

    原文:SQL开发中容易忽视的一些小地方(四) 本篇我想针对网上一些对于非聚集索引使用场合的某些说法进行一些更正. 下面引用下MSDN对于非聚集索引结构的描述. 非聚集索引结构: 1:非聚集索引与聚集索 ...

  7. Device &quot;/dev/sdg&quot; is not a partition【再续】

    之前创建asm磁盘的时候总结过一下错误:http://blog.csdn.net/rhys_oracle/article/details/17029333 当今天情况是这种.例如以下: 在使用open ...

  8. Java新手如何学习Spring、Struts、Hibernate三大框架?(转)

    整理知乎大牛答案: 1.入门看文档(blog,书籍等等),深入理解配置文件的含义(Spring.Struts.Hibernate); 2.遇到问题,自己动手解决,如果解决了,为什么这样解决?(凡事总问 ...

  9. tomcat如何避免遭遇ClassNotFoundException

    于Tomcat紧接着为什么要创建一个类加载器Thread.currentThread().setContextClassLoader(catalinaLoader)?这里加载失败主要是为了避免以后加载 ...

  10. HDU 1754 I Hate It (段树 &amp; 树阵)

    I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...