矩阵螺旋遍历Spiral Matrix,Spiral Matrix2
SpiralMatrix:
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]
.
算法分析:其实就是两重循环遍历,每次都是遍历一圈,然后遍历里面一圈...,需要记录m,n,每次遍历后,m,n都减去2;还要记录坐标x,y;
特例是n=1;m=1;其实m,n为奇数才有这种特例。
public class SpiralMatrix
{
public List<Integer> spiralOrder(int[][] matrix)
{
List<Integer> res = new ArrayList<>();
if(matrix.length == 0 || matrix == null)
{
return res;
}
int m = matrix.length;
int n = matrix[0].length;//得先判断矩阵是否为空,否则数组下标越界
int x = 0, y = 0;
while(m > 0 && n > 0)
{
if(m == 1)
{
for(int i = 0; i < n; i ++)
{
res.add(matrix[x][y++]);
}
break;//别忘了break
}
else if(n == 1)
{
for(int j = 0; j < m; j ++)
{
res.add(matrix[x++][y]);
}
break;
} for(int i = 0; i < n-1; i ++)
{
res.add(matrix[x][y++]);
}
for(int i = 0; i < m-1; i ++)
{
res.add(matrix[x++][y]);
}
for(int i = 0; i < n-1; i ++)
{
res.add(matrix[x][y--]);
}
for(int i = 0; i < m-1; i ++)
{
res.add(matrix[x--][y]);
}
x++;
y++;
m -= 2;
n -= 2;
}
return res;
}
}
SpiralMatrix2:
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
算法分析:和上一算法,类似,只不过这是个n*n矩阵,其实可以延伸到m*n矩阵。n*n使清空更简单,特别是n=1的时候。
public class SpiralMatrix2
{
public int[][] generateMatrix(int n)
{
int[][] res = new int[n][n];
if(n == 0)
{
return res;
}
int x = 0, y = 0;
int val = 1;
while(n > 0)
{
if(n == 1)
{
res[x][y] = val;
break;
}
for(int i = 0; i < n-1; i ++)
{
res[x][y++] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x++][y] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x][y--] = val;
val ++;
}
for(int i = 0; i < n-1; i ++)
{
res[x--][y] = val;
val ++;
}
x ++;
y ++;
n -= 2;
}
return res;
}
}
矩阵螺旋遍历Spiral Matrix,Spiral Matrix2的更多相关文章
- 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@ [54/59] Spiral Matrix & Spiral Matrix II
https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- [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 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- [Leetcode] spiral matrix ii 螺旋矩阵
Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...
随机推荐
- python之MySQL学习——简单的增删改查封装
1.增删改查封装类MysqlHelper.py import pymysql as ps class MysqlHelper: def __init__(self, host, user, passw ...
- Powershell调用RemoteExchange.ps1
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010" ...
- php accumulation rockmongo
php -r 'echo substr(sprintf("%o",fileperms("./")),-4);'
- Linux上添加服务(htttpd,samba等等)
service httpd restart 出现 httpd:unrecognized service 错误 vi /etc/rc.d/rc.local #在/etc/rc.d/rc.local中 ...
- 模块 - time/datetime
time 模块 time模块方法: >>> import time >>> time.time() #时间戳 秒级别 1519212085.6211221 #从19 ...
- java 字符串解析为json 使用org.json包的JSONObject+JSONArray
参考: https://blog.csdn.net/xingfei_work/article/details/76572550 java中四种json解析方式 JSONObject+JSONArray ...
- js实现模糊查询
1.简述 实现模糊查询方法有很多种,后端可以实现,前端使用js也可以实现. 后端实现起来需要根据输入框中搜索的关键字,去后台拼接SQL语句查询. 前端直接使用字符串的indexOf()方法或者正则表达 ...
- sql server代理系列
https://www.cnblogs.com/woodytu/tag/SQL%20Server%E4%BB%A3%E7%90%86/ SQL Server代理(12/12):多服务器管理 Woody ...
- eslasticsearch操作集锦
索引-index:一个索引就是一个拥有几分相似特征的文档的集合.比如说,你可以有一个客户数据的索引,另一个产品目录的索引,还有一个订单数据的索引.一个索引由一个名字来标识(必须全部是小写字母的),并且 ...
- ufs emmc
UFS 2.0闪存标准使用的是串行界面,很像PATA.SATA的转换.并且它支持全双工运行,可同时读写操作,还支持指令队列. eMMC是半双工,读写必须分开执行,指令也是打包的. 而且UFS芯片不仅传 ...