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 ...
随机推荐
- 系统如何端子app弄root才干
最近由于调试USB OTG怪东西.这导致USB端口被占用,这项.虽然我是project版本号,但不能运行adb shell,这是不可能的debug该. 所以,我现在是一个系统终端apk,规划 http ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 文章13称号 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Apache 2.4虚拟主机配置
托管它指的是多个站点的执行一台机器上 (例如 company1.example.com 和 company2.example.com) . 机能够"基于 IP",即每一个 IP 一 ...
- 第三章——使用系统函数、存储过程和DBCC SQLPERF命令来监控SQLServer(3)
原文:第三章--使用系统函数.存储过程和DBCC SQLPERF命令来监控SQLServer(3) 本文为这个系列最后一篇.将是如何使用DBCC命令来监控SQLServer日志空间的使用情况. 前言: ...
- spring整合redis客户端及缓存接口设计(转)
一.写在前面 缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存.有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器.但是无论使用哪种缓存,接口中的方法都是差不多. ...
- mysql经常使用命令总结
MySQL经常使用指令(备查) 最经常使用的显示命令: 1.显示数据库列表. show databases; 2.显示库中的数据表: use mysql; show tables; 3.显示数 ...
- 使用live delegate on解决js后装html故障问题
今天写一个前端的东西.每学到更多的知识.几下就能写几行代码.代码行数十个.代码几个文件量--这是真的.一直以来研究的前端遇到的问题仍然在实践百度谷歌问答. 我今天遇到这样的问题:已经写js代码,正确a ...
- 【转】Oracle Outline使用方法及注意事项
概要 Oracle Outline是用来保持SQL运行计划(execution plan)的一个工具. 我们能够通过outline工具防止SQL运行计划在数据库环境变更(如统计信息,部分參数等)而引 ...
- Unity项目优化--开发项目的小经验
原文地址:http://blog.csdn.net/liang_704959721/article/details/8548619 我们主要使用 3dsmax2010 进行制作,输出 FBX的类型导入 ...