54.Spiral Matrix

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

好久没做题了,博客也好久没更新,今天来一发水题,话说这题有点像poj的滑雪题.

传送门POJ:http://poj.org/problem?id=1088

题解:http://www.cnblogs.com/dick159/p/5258943.html

先说一下这题吧,就是分四个方向,然后从外到里按照顺序遍历,要注意结束条件.(我设置成访问过的值就不访问了)

ps:要判断一下间隔了的2个方向是否发生过移动,如果没有发生过移动则表示遍历已完毕.(这里我就用list的大小是否发生了变化来判断.)

目前没有想到更好的方法。【太笨了(╯﹏╰)】

Java Code:

public class Solution {

        public List<Integer> spiralOrder( int[][] matrix ) {
List<Integer> list = new ArrayList<Integer>();
if( matrix == null || matrix.length == 0 )
return list;
int m = matrix[ 0 ].length;
int n = matrix.length;
if( matrix[ 0 ].length == 1 ) {
for( int i = 0; i < n; i++ ) {
list.add( matrix[ i ][ 0 ] );
}
return list;
} if( matrix.length == 1 ) {
for( int i = 0; i < m; i++ ) {
list.add( matrix[ 0 ][ i ] );
}
return list;
}
int l = 0;
int preSize = 0;
int i = -1;
while( matrix[ l ][ l ] != -Integer.MAX_VALUE ) {
preSize = list.size();
for( i = l; i < m - l; i++ ) {
list.add( matrix[ l ][ i ] );
matrix[ l ][ i ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = l + 1; i < n - l; i++ ) {
list.add( matrix[ i ][ m - l - 1 ] );
matrix[ i ][ m - l - 1 ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = m - l - 2; i >= l; i-- ) {
list.add( matrix[ n - l - 1 ][ i ] );
matrix[ n - l - 1 ][ i ] = -Integer.MAX_VALUE;
}
if(preSize == list.size()){
return list;
}
preSize = list.size();
for( i = n - l - 2; i >= l + 1; i-- ) {
list.add( matrix[ i ][ l ] );
matrix[ i ][ l ] = -Integer.MAX_VALUE;
}
l++;
}
return list;
}
}

[LeetCode]Spiral Matrix 54的更多相关文章

  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 螺旋矩阵

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

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

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

  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 解题报告

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

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

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

  7. LeetCode——Spiral Matrix

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

  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. 简述Android系统内存不足时候,内存回收机制

    当Android系统的内存不足时,会根据以下的内存回收规则来回收内存: 1.先回收与其他Activity或Service/Intent Receiver无关的进程(即优先回收独立的Activity) ...

  2. Swift之UITabBarController 导航控制器颜色的改变

    废话不多 直接上代码 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) self.window!.backgroundColor ...

  3. python如何安装模块

    1.从 https://pypi.python.org/pypi/XXXX  下载压缩包 2.解压所下载的压缩包 3.CD到解压目录,执行 sudo python setup.py install

  4. java程序的工作原理

    Sun公司设计java语言的目标是让Java程序不必经过修改就可以在各种各样的计算机(包括PC机和工作站)上运行.为了实现这一目标,Sun公司提供了一阵Java虚拟机(Java Virtual Mac ...

  5. Flex中escape/encodeURI/encodeURIComponent的区别

    Flex中提供了三种转码函数,各有各的区别, escape,encodeURI,encodeURIComponent 这三个函数不仅在flex中有道运用在javascript中同样的含义 ,今天我仔细 ...

  6. ASP.NET Zero--12.一个例子(5)商品分类管理-编辑分类

    1.添加编辑按钮 打开文件Index.js [..\MyCompanyName.AbpZeroTemplate.Web\Areas\Mpa\Views\Category\Index.js] 在acti ...

  7. css块级标签,行内标签,行内块标签的转换(2)

            css块级标签,行内标签,行内块标签的转换 版权声明 本文原创作者:雨点的名字 作者博客地址:https://home.cnblogs.com/u/qdhxhz/ 在基础1中,我详细讲 ...

  8. 前端自动化构建工具-yoman浅谈

    如今随着前端技术的飞速发展,前端项目也变得越来越复杂. 快速的搭建一个集成多种工具和框架的复杂前端项目也越来越成为一种需求. 当然如果你要自己从0开始完全自己diy,绝对可以,只不过需要耗费一些不少的 ...

  9. Myeclipse8.5 反编译插件 jad 安装(转)

    准备工作 下载jad.exe文件:http://www.varaneckas.com/sites/default/files/jad/jad158g.win.zip 下载jadeclipse插件:ht ...

  10. HDU4738(割边)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...