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]
.
原题链接:https://oj.leetcode.com/problems/spiral-matrix/
向右螺旋式遍历。
public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix.length == 0)
return null;
List<Integer> list = new ArrayList<Integer>();
int l = 0, r = matrix[0].length - 1;
int u = 0, d = matrix.length - 1;
while (l <= r && u <= d) {
for (int i = l; i <= r; i++)
list.add(matrix[u][i]);
u++;
if (u > d)
continue;
for (int i = u; i <= d; i++)
list.add(matrix[i][r]);
r--;
if (l > r)
continue;
for (int i = r; i >= l; i--)
list.add(matrix[d][i]);
d--;
if (u > d)
continue;
for (int i = d; i >= u; i--)
list.add(matrix[i][l]);
l++;
}
return list;
}
}
LeetCode——Spiral Matrix的更多相关文章
- 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 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 解题报告
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 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 ...
随机推荐
- BZOJ3362 [Usaco2004 Feb]Navigation Nightmare 导航噩梦
标题效果:自脑补. 思维:与维护两个维度和可设置为检查右. 注意,标题给予一堆关系的.我们应该加入两对关系. Code: #include <cstdio> #include <cs ...
- iOS pragma mark要使用
郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 简单的来说 ...
- zabbix 实现curl 显示器
1.进入Configure->Templates 2. 新建一个模板 3.新建模板,并保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFpND ...
- FZU 1686 龙之谜 重复覆盖
兑换0,1模型,如.注意,数据的范围 #include <stdio.h> #include <string.h> #include <iostream> #inc ...
- 深入理解Linux修改hostname(转)
当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...
- BZOJ 1212 HNOI2004 L语言 AC自己主动机(Trie树)+动态规划
标题效果:给定词的列表,并m串 每个字符串q个最长前缀,这个前缀可满足拆分成一些字符串 这些字符串中存在的词汇太 再也不怕错误的数据范围--有一个很明显Trie树能解决的问题竟然被我写的AC自己主动机 ...
- hdu 5072 Coprime(同色三角形+容斥)
pid=5072">http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了.感觉好可惜 ...
- eclipse字母大写和小写转换的快捷键
大写转换小写 ctrl+shift+y 小写转换大写 ctrl+shift+x
- thinkphp学习笔记3—项目编译和调试模式
原文:thinkphp学习笔记3-项目编译和调试模式 1.项目编译 在章节2.4项目编译中作者讲到使用thinkphp的项目在第一次运行的时候会吧核心需要加载的文件去掉空白和注释合并到一个文件中编译并 ...
- 工作介绍xml书包文件
光开放平台一个非常重要的特点就是简化了对xml文件的操作,您能非常轻松地引入xml文件.定位到随意节点.增删属性和文本以及节点本身,以下咱们用实例来介绍对xml的操作 引入xml文件: <cht ...