[算法][LeetCode]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].
分析
Java代码
public static ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> order = new ArrayList<Integer>();
if (matrix.length == 0 || matrix[0].length == 0) return order;
int xMin = 0;
int yMin = 0;
int xMax = matrix[0].length - 1;
int yMax = matrix.length - 1;
order.add(matrix[0][0]);
int i = 0, j = 0;
while (true) {
while (i < xMax) order.add(matrix[j][++i]);
if (++yMin > yMax) break;
while (j < yMax) order.add(matrix[++j][i]);
if (xMin > --xMax) break;
while (i > xMin) order.add(matrix[j][--i]);
if (yMin > --yMax) break;
while (j > yMin) order.add(matrix[--j][i]);
if (++xMin > xMax) break;
}
return order;
}[算法][LeetCode]Spiral Matrix的更多相关文章
- [算法][LeetCode]Spiral Matrix——螺旋矩阵
题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...
- 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 II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 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 解题报告
Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix ...
- 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 54
54.Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the ma ...
- [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 ...
随机推荐
- IntelliJ IDEA使用教程三 SVN的集成与使用
注意: 虽然IDEA已经集成了svn客户端,但还是习惯使用第三方svn客户端,比如: TortoiseSVN. 就是因为使用的是第三方客户端,所以和IDEA集成的时候就出现了一个特别大的坑,因为svn ...
- linux strace追踪mysql执行语句 (mysqld --debug)
转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...
- 配置NGINX支持中文URL 中文文件名称或文件夹404无法訪问的解决方法
NGINX不须要象apache那样要单独载入支持中文模块. # cat /etc/sysconfig/i18n LANG="en_US.UTF-8" SYSFONT=" ...
- Linux系统不可不知的命令:sudo
转自:http://www.embeddedlinux.org.cn/html/xinshourumen/201308/05-2608.html sudo是个统管一切的命令.它的字面意思是代表“超级用 ...
- 转 ios给view设置圆角
// 圆角 userhead.layer.masksToBounds = YES; userhead.layer.cornerRadius = 6.0; userhead.layer.borderWi ...
- 在oracle中查询已知表名的表中所有字段名,每个字段是否是主键,是否是外键,是否为空的sql语句
查询表的所有列及其属性:select t.*,c.COMMENTS from user_tab_columns t,user_col_comments c where t.table_name = c ...
- 【BZOJ】【1178】【APIO2009】convention会议中心
贪心 如果不考虑字典序的话,直接按右端点排序,能选就选,就可以算出ans…… 但是要算一个字典序最小的解就比较蛋疼了= = Orz了zyf的题解 就是按字典序从小到大依次枚举,在不改变答案的情况下,能 ...
- 关于VS 工具箱灰色,不可用的解决方案
使用vs的命令行工具,在命令行中运行:devenv /ResetSkipPkgs ,重新打开vs,重置一下工具箱 ,OK,成功了~! 希望能对大家有帮助!
- Android中Intent的显示和隐式使用
Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件, ...
- Insert Interval leetcode java
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...