矩阵螺旋遍历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 ...
随机推荐
- linux ftp 上传与下载命令解析
month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...
- 170302、 Apache 使用localhost(127.0.0.1)可以访问,使用本机局域网IP(192.168.2.*)不能访问
对于此问题的解决办法,打开apache安装路径中的http.conf文件, 找打以下内容 # onlineoffline tag - don't remove Order Deny, ...
- Centos7 下安装mysql数据库
centos7系统,安装mysql发现已经默认的是mariadb. 只能安装mariadb,mariadb是mysql一个分支,对mysql完全支持 1 安装 yum -y install maria ...
- MongoDB-6: MongoDB索引
一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...
- Andrew Ng机器学习编程作业:Anomaly Detection and Recommender Systems
作业文件 machine-learning-ex8 在本次练习,第一节我们将实现异常检测算法,并把它应用到检测网络故障服务器上.在第二部分,我们将使用协同过滤来构建电影推荐系统. 1. 异常检测 在这 ...
- golang SQLite3性能测试
SQLite是个小型的数据库,很简洁,即支持文件也支持内存,比较适合小型的独立项目,在没有网络的时候做一些复杂的关系数据存储和运算. 为了考察性能做10M(1000万)条记录的测试,测试机4CPU.8 ...
- Web框架简介
Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- React:快速上手(5)——掌握Redux(2)
React:快速上手(5)——掌握Redux(2) 本文部分内容参考阮一峰的Redux教程. React-Redux原理 React-Redux运行机制 我觉得这张图清楚地描述React-Redux的 ...
- python约束 与MD5加密写法
python 中约束写法有两种 1 常用的通过继承关系主动抛出异常 2 通过抽象类+抽象方法 1 常用的通过继承关系主动抛出异常写法 在本send方法中报错不会抛出异常, class BaseMes ...
- hadoop单击模式环境搭建
一 安装jdk 下载相应版本的jdk安装到相应目录,我的安装目录是/usr/lib/jdk1.8.0_40 下载完成后,在/etc/profile中设置一下环境变量,在文件最后追加如下内容 expor ...