Spiral Matrix 解答
Question
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].
Solution
这道题只有笨方法,就是老老实实地遍历输入的二维数组,得到结果。
但是在遍历时有个技巧,就是按照环来遍历。(x, y)为遍历点的坐标。我们发现,当遍历完一圈后,(x, y)又会回到起点。所以对于接下来里面一圈的遍历,只需x++, y++即可。

由于输入不一定是正方形,有可能最后剩的一圈是一行或一列,此时根据省下的行数/列数判断,另行处理。
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<Integer>();
if (matrix == null || matrix.length < 1)
return result;
int m = matrix.length, n = matrix[0].length, x = 0, y = 0;
while (m > 0 && n > 0) {
// If only one row left
if (m == 1) {
for (int i = 0; i < n; i++)
result.add(matrix[x][y++]);
break;
}
// If only one column left
if (n == 1) {
for (int i = 0; i < m; i++)
result.add(matrix[x++][y]);
break;
}
// Otherwise, we traverse in a circle
// Left to Right
for (int i = 0; i < n - 1; i++)
result.add(matrix[x][y++]);
// Up to Down
for (int i = 0; i < m - 1; i++)
result.add(matrix[x++][y]);
// Right to Left
for (int i = 0; i < n - 1; i++)
result.add(matrix[x][y--]);
// Down to Up
for (int i = 0; i < m - 1; i++)
result.add(matrix[x--][y]);
x++;
y++;
m -= 2;
n -= 2;
}
return result;
}
}
Spiral Matrix 解答的更多相关文章
- Spiral Matrix II 解答
Question Given an integer n, generate a square matrix filled with elements from 1 to n2 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 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- 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 ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
随机推荐
- Saruman's Army (POJ 3069)
直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在 ...
- javaDay1 基础知识
常用dos命令 •d: 回车 盘符切换 •dir(directory):列出当前目录下的文件以及文件夹 •md (make directory) : 创建目录 •rd (remove directo ...
- python中os模块常用方法
#!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...
- Struts2 页面url请求怎样找action
1.我们使用最原始的方法去查找action.不同注解. struts.xml文件先配置 <!-- 新闻信息action --> <action name="newsInfo ...
- resin安装和配置
1 从 http://www.caucho.com/download/ 下载resin 2 检查你的linux环境,查看是否安装了 jdk1.5 或以上版本,是否安装了perl. 输入命令:Java ...
- linux下安装软件的方法
1. 区分 rpm -qi -qf -ql -qa四个不同选项组合的作用?rpm -qi //查询已经安装的某个RPM软件包的信息rpm -qf //查询某个程序文件是由哪个RPM软件包安装的rpm ...
- (转) [老老实实学WCF] 第三篇 在IIS中寄存服务
第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我们对WCF的编程生 ...
- Qt串口通信
1. Qt串口通信类QSerialPort 在Qt5的的更新中,新增了串口通信的相关接口类QSerialPort,这使得在开发者在使用Qt进行UI开发时,可以更加简单有效地实现串口通信的相关功能. 开 ...
- silverlight .net后台 设置visifire控件图表样式 属性说明
.net后台 代码: 如图 Chart chart = new MyCharts(); //图表 //chart.Watermark = false; //没好使 ...
- 委托,匿名方法,Lambda,泛型委托,表达式树
一.委托:完成一个委托应分三个步骤://step01:首先用delegate定义一个委托;public delegate int CalculatorAdd(int x, int y);//step0 ...