矩阵螺旋遍历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 ...
随机推荐
- 模态窗口原理及注意事项--http://www.alisdn.com/wordpress/?p=53
前言 在开发Windows引用程序的时候,在一些需要用户确认,或者提示用户注意的场合,经常使用模态对话框,或者叫模态窗口.在绝大多数情况下,模态窗口给开发人员带来了极大的便利,并且在某些应用上有不可替 ...
- Openstack深入了解虚拟机
续Openstack虚拟机创建流程 在控制节点也安装一个计算服务也变成一个计算节点 yum -y install openstack-nova-compute 启动并且设置开机启动 systemctl ...
- 10.php引用(&)详解及注意事项
<?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条 ...
- how to deal with ^M in linux
change windows file to linux file dos2unix configure https://blog.csdn.net/xiongmaojiayou/article/de ...
- Docker Libnetwork Bridge插件实现代码分析----初始化部分
Bridge driver数据结构如下所示: type driver struct { config *configuration network *bridgeNetwork natChain *i ...
- 我的Android进阶之旅------>Java字符串格式化方法String.format()格式化float型时小数点变成逗号问题
今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.好吧,又是我来维护. 好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章 我的And ...
- hibernate自动建表之engine设置
1.MYSQL的数据库引擎中,只有InnoDB和BDB(Berkley DB )包括了对事务处理和外键的支持.如果数据引擎建为MyISAM则rollback无效. 2.而hibernate自动建表的时 ...
- 简明python教程二-----对象
物理行:是你在编写程序时所看见的. 逻辑行:是Python看见的单个语句. 默认的,python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一个物理行中使用多于一个逻辑行,用分号“: ...
- 第三课 Makefile文件的制作(上)
1.序言: 前面的课程讲解了从gcc编译过程到其实践,大家可以看到其实在这些步骤中有些是可以简化编译的,但由于参数多以及项目中文件数量多的原因难免会造成错误甚至是浪费大量的时间在这编译上,为此linu ...
- UVALive 6906 A - Cluster Analysis
思路:排个序,依次选就好了. #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namesp ...