[LeetCode]Spiral Matrix 54
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的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- LeetCode: Spiral Matrix 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
- LeetCode——Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
- [leetcode]Spiral Matrix @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix/ 题意: Given a matrix of m x n elements (m rows, n ...
随机推荐
- Delphi中ShellExecute的妙用
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制.有几个API函数都可以实现这些功能,但是在大多数情况下Shel ...
- js模块化开发——require.js的实战写法1
关于在Require.js使用一个JS插件的问题 我需要在项目中引用一个js控件,这个控件依赖于a.js,b.js,c.js,.....n.js N多个js以及jquery及jquery-ui,各js ...
- delphi popupmenu控件用法
是,右键菜单控件,和特定的窗体控件的popmenu属性关联就可以了 添加一个popupmenu控件,双击该控件,在弹出的界面中设置好name以及caption属性,点击事件的做法就跟button一样了 ...
- cygwin下配置alias
主要是cygwin下的盘符映射关系容易忘,直接用mount命令: 所以,找到E:\cygwin64\home\Administrator下面的.bashrc,添加alias.如下:
- MySQL 替换部分电话号码为000
要做敏感信息剔除,要求又不能全换成同一个号码影响测试,想了几个方法,最终采用替换部分电话号码为000来做到敏感信息覆盖. mysql>update phone setb=replace(b,su ...
- 【转载】doxygen+graphviz生成代码文档
一.工具 doxygen:http://www.stack.nl/~dimitri/doxygen/download.html graphviz:http://www.graphviz.org/ 二. ...
- 必应地图api文档,微软必应地图web开发版详解,可以在国内使用国外地图
最近,公司项目要求在页面中嵌入地图,需求还算简单,但是由于必须具备响应式(主要是pc和移动端),而且由于公司业务是全球性的,要支持国外地点搜索.考虑到百度,腾讯,高德等等国内地图无法显示国外数据,谷歌 ...
- js冒泡排序及计算其运行时间
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Nagios 系统监控基本安装配置过程详解
Nagios 是一款免费的开源 IT 基础设施监控系统,功能强大,灵活性强,能有效监控 Windows.Linux.VMware 和 Unix 主机状态,交换机.路由器等网络设置等.一旦主机或服务状态 ...
- 2017qq红包雨最强攻略,
这个只支持苹果手机,而且要有苹果电脑,只有苹果手机是不行的. QQ红包规则:只要你到达指定的位置,就可以领取附近的红包,一般也就几毛,还有几分的,当然也不排除有更高的,只不过我是没遇到... 那么既然 ...