[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 ...
随机推荐
- matlab计算矩阵每列非0元素个数
在统计分析中,有时候需要计算矩阵每列非0元素的个数,可以用以下方法: 先用find找到每列不为0的元素index,然后用count计数. 假设有矩阵A[M,N], 结果存在countZeros cou ...
- 如何改变xls中的单元格左上角的图标
点绿色小三角的是文本型数字,是不能参与加减运算的.首先选中含有绿色小三角的单元格,右击鼠标选择,设置单元格格式, 数字选项卡,选择常规
- 6.00.1x Introduction to computation
6.00 Introduction to Computer Science and Programming • Goal: –Become skillful at ...
- MEAN教程2-Nodejs安装
安装Node.js稳定版本最简单的办法也是使用二进制文件,Node.js官方网站上提供了下载地址,可用于Linux.Mac OS X和Windows系统.同样要注意下载与目标操作系统架构一致的文件. ...
- gulp实时编译less,压缩合并requirejs模块文件
gulp的使用命令简单,就几个,gulp的简单使用教材可以参考一点的gulp使用教材(http://www.ydcss.com/archives/18). 下面就简单的介绍这些命令如何互相配合的完成前 ...
- viewpager翻页的窗帘效果动画
前端时间比较忙,好长时间没有更新微博,就工作中出现的部分问题,与大家分享一下. 大家都知道viewpager在android开发中是运用率比较高的控件,现在就其窗帘下过的动画分享. 文章出处:http ...
- ArcGIS Pro 简明教程(1)Pro简介
ArcGIS Pro 简明教程(1)Pro简介 ArcGIS Pro已经发布了相当的一段时间了,截至笔者写这系列文章的时候已经是1.3版本了,已经是相当完善的一个版本,基本上已经完成了原来ArcGIS ...
- JAVA构造函数的继承
1.子类中无参构造函数,可直接继承父类中无参构造函数,前提是所有变量均为public 如下:父类Student中有空构造函数Student(),子类Pupil中有空构造函数Pupil(),后者会继承前 ...
- look look C#7
vs2017也rc好几个版本了,本想跟进看看c#7加入了什么内容,去搜索c#7,确实找到了不少文章,无奈很多特性ide根本不让编译啊...所以今天主要列出已经确定了的c#7特性(一般来说rc后也不会加 ...
- (原创)Java多线程作业题报java.lang.IllegalMonitorStateException解决
作业: 有一个水池,水池容量500L,一边为进水口,一边为出水口,要求进水放水不能同时进行,水池一旦满了不能继续注水,一旦空了,不能继续放水,进水速度5L/s,放水速度2L/s. 这是我学多线程时做的 ...