Leetcode: Spiral Matrix. Java
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].
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
int m = matrix.length;
if(m == 0) return res;
int n = matrix[0].length;
//循环次数小于m/2和n/2
for(int i = 0; i < m/2 && i < n/2; i++){
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[i][i+j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[i+j][n-i-1]);
for(int j = 0; j < n - 1 - i * 2; j++)
res.add(matrix[m-i-1][n-i-1-j]);
for(int j = 0; j < m - 1 - i * 2; j++)
res.add(matrix[m-i-1-j][i]);
}
//循环结束后假设行数/列数是奇数,则还剩一行/列
if(m % 2 != 0 && m <= n){
for(int j = 0; j < n - (m/2) * 2; j++)
res.add(matrix[m/2][m/2+j]);
}
else if(n % 2 != 0 && m > n){
for(int j = 0; j < m - (n/2) * 2; j++)
res.add(matrix[n/2+j][n/2]);
}
return res;
}
}
Leetcode: Spiral Matrix. Java的更多相关文章
- 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 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 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 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 ...
- [leetcode]Spiral Matrix II @ Python
原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...
随机推荐
- MFC消息响应机制分析
---- 摘要: ---- MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,我们在这里,对它的整个消息映射机制进行了系统的分析 ...
- 关于Delphi中的字符串的浅析(瓢虫大作,里面有内存错误的举例)
关于Delphi中的字符串的浅析 只是浅浅的解析下,让大家可以快速的理解字符串. 其中的所有代码均在Delphi7下测试通过. Delphi 4,5,6,7中有字符串类型包括了: 短字符串(Short ...
- Aerospike | Aerospike Chinese
Aerospike | Aerospike Chinese 如果您的企业依赖于: 庞大的数据量(超过任何结构化数据库所能处理的数据量) 可预见(且快速)的性能 透明的扩展 始终正常运行 那么您只有一个 ...
- Tengine中的proxy_upstream_tries
upsream xxx { server 192.168.100.100; server 192.168.100.101; server 192.168.100.102; } server { loc ...
- 与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态
原文:与众不同 windows phone (23) - Device(设备)之硬件状态, 系统状态, 网络状态 [索引页][源码下载] 与众不同 windows phone (23) - Devic ...
- osgEarth开发之OSG解构——失败的尝试
概述 本文在吸收了<最长的一帧>以及相关参考资料的基础之上解读OSG的基础数据结构,渲染方法. 实现 在这第一部分里,要理解的是run函数的实现,因为以下这一段证明了它的重要性和强大的能力 ...
- poj1639 Picnic Planning 最小度数限制生成树
题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...
- 取CPU序列号,获取网卡,取硬盘系列号,获取目录下的文件,强制删除目录
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Google 搜索的基本语法
★搜索引擎的选择 先简单说一下"搜索引擎的选择". 在咱们天朝,Google 屡屡被 GFW 骚扰,导致百度占了便宜,成为份额最高的搜索引擎.不过今天这篇教程,俺还是继续拿 Goo ...
- osc搜索引擎框架search-framework,TngouDB,gso,
项目目的:OSChina 实现全文搜索的简单封装框架 License: Public Domain 包含内容: 重建索引工具 -> IndexRebuilder.java 增量构建索引工具 -& ...