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 ...
随机推荐
- OLE操作Excel编译错误处理
Excel在公司用的很多,而这个东西我用的不是很好,就想用程序来处理,遇到很多错误.这几天研究了下OLE操作Excel.环境:VS2008 SP1+Excel 2007 加入OLE Type Li ...
- IPMI 配置BMC用户设置
IPMI 配置BMC用户设置 本文档共介绍5条ipmi设置user的命令,这些命令需要使用root权限才能使用,其中- H为需要操作的BMC ip,-I lanplus为使用rmcp+协议发送命令,- ...
- ie8 hack
1.‘\9’: eg:.test { color/*\**/: blue\9 }.header {width:300px;} /* 所有浏览器*/.header {width/*\**/:330px\ ...
- seajs初尝 加载jquery返回null解决学习日志含示例下载
原文地址:http://www.tuicool.com/articles/bmuaEb 如需demo示例,请点击下方链接下载: http://yunpan.cn/cVEybKs8nV7CF 提取码 ...
- cobar和tddl分享
Cobar是阿里巴巴(B2B)部门开发的一种关系型数据的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务.那么具体说说我们为什么要用它,或说cobar--能干什么?以下 ...
- IOS 图片模糊处理 ------ 直接代码 复制出去就可用 值得标记
1. UIImage *imag = [UIImage imageNamed:@"img"]; /* --------------------使用 coreImg ------- ...
- ios文件读取(二)
- (void)viewDidLoad { [super viewDidLoad]; /** * @brief 获取文件路径 * */ NSString * filePath = [self get ...
- Couchbase 使用方法
1.数据流向 List<模型> 数据-->MsgPack 打包成byte[]-> couchbase 实例调用 Store(Enyim.Caching.Memcached.S ...
- DOM 节点实例操作
涉及知识点包括节点的所有知识 目的: 自动为文档创建一个目录表 自动创建目录
- Hbase常见异常
1. HBase is able to connect to ZooKeeper but the connection closes immediately hbase(main):001:0> ...