[算法][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 ...
随机推荐
- Python168的学习笔记4
关于普通文本文件的读写 python2.7中,未注明的字符都是以acsii来编码的,而要让字符能够通用,必须声明为unicode. s=u'你好',s.encode('utf8')就是指用utf8来进 ...
- BZOJ 4421: [Cerc2015] Digit Division 排列组合
4421: [Cerc2015] Digit Division 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4421 Descripti ...
- java编译优化
#java编译器对`String常量表达式`的优化: - 1.String+String 可以被编译器识别为常量表达 String a="ab" ; String b=" ...
- The sigrok project
http://www.sigrok.org/wiki/Main_Page The sigrok project aims at creating a portable, cross-platform, ...
- golang 字符串与整数, 布尔转换 strconv
strconv 是golang对于字符串和基本数据类型之间的转换字符串转整数testStr := "1000" testInt, err := strconv.Atoi(testS ...
- .NET泛型01,为什么需要泛型,泛型基本语法
.NET泛型或许是借鉴于C++泛型模版,借助它可以实现对类型的抽象化.泛型处理,实现了类型和方法之间的解耦.一个最经典的运用是在三层架构中,针对不同的领域模型,在基接口.基类中实现针对各个领域模型的泛 ...
- UIView 精要概览(持续更新)
--1-- 知识点:为UIView 设置圆角 前提:layer 属性需要 <QuartzCore/QuartzCore.h> 静态库的支持,所以需要提前导入到项目中,并在你的文件中包含#i ...
- ExtJS创建选项卡
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Jquery焦点图/幻灯片效果 插件 KinSlideshow
JavaScript $(function(){ $("#KinSlideshow").KinSlideshow({ moveSty ...
- struts2怎么实现页面到页面之间的传值?
我要实现一个产品订购的功能,在浏览产品的时候通过点击一个订购的链接,跳转到提交订单的页面,在跳转的同时要把浏览的产品的名称和型号传到提交订单的页面,并且把这里的订单类的产品名称和型号的表单域里赋上传递 ...