矩阵螺旋遍历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 ...
随机推荐
- 160705、总结:commons-codec.jar中常用方法
一.Base64编码和解码import org.apache.commons.codec.EncoderException;import org.apache.commons.codec.binary ...
- MySQL中的注释(有三种)
MysQL支持三种注释: .#... (推荐这种,具有通性) ."-- ..." (注意--后面有一个空格) ./*...*/
- 17.Recflection_反射
www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html
- query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...
- 第09章—使用Lombok插件
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- Tomcat WEB站点部署
上线的代码有两种方式, 第一种方式是直接将程序目录放在webapps目录下面 第二种方式是使用开发工具将程序打包成war包,然后上传到webapps目录下面.下面让我们见识一下这种方式 这个网站里面已 ...
- 华为大数据项目fusionInsight
项目简述:基于开源Hadoop2.0架构的集群网络,进行海量数据的分布式计算.由于Hadoop集群规模不断扩大,而搭建一个同等规模的测试集群需要一笔昂贵的开销.目前有100台左右物料,期望预测计算节点 ...
- yum速查
yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包, 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖 ...
- directorjs和requirejs和artTemplate模板引擒建立的SPA框架
分为4块:A : index.html壳子. 加载B init-config.js, 加载D header.html模板B : init-config.js 个人信息+路由配置+权限+渲 ...
- LightOJ - 1356 Prime Independence (数论+二分图匹配)
题意:有N个数的集合,其中选出若干个数组成一个子集,要求这个子集中的任意两个数a,b都不能通过a=k*b得到,其中k是一个素数.求这个子集最大的size. 分析:集合中任意两数的关系是二者之间是否之差 ...