Spiral Matrix leetcode 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]
.
题解:
这道题是实现题。
考虑2个初始条件,如果矩阵只有一行或者一列,那么无需转圈,依次输出即可。
其他情况均需转圈:从左到右,从上到下,从右到左,从下到上。 从大圈依次循环到小圈即可。
代码如下:
1 public ArrayList<Integer> spiralOrder(int[][] matrix) { 2 ArrayList<Integer> result = new ArrayList<Integer>(); 3 if(matrix == null || matrix.length == 0) 4 return result; 5 6 int m = matrix.length; 7 int n = matrix[0].length; 8 9 int x=0; int y=0; while(m>0 && n>0){ //if one row/column left, no circle can be formed if(m==1){ for(int i=0; i<n; i++){ result.add(matrix[x][y++]); } break; }else if(n==1){ for(int i=0; i<m; i++){ result.add(matrix[x++][y]); } break; } //below, process a circle //top - move right for(int i=0;i<n-1;i++) result.add(matrix[x][y++]); //right - move down for(int i=0;i<m-1;i++) result.add(matrix[x++][y]); //bottom - move left for(int i=0;i<n-1;i++) result.add(matrix[x][y--]); //left - move up for(int i=0;i<m-1;i++) result.add(matrix[x--][y]); x++; y++; m=m-2; n=n-2; } return result; }
Reference:http://www.programcreek.com/2013/01/leetcode-spiral-matrix-java/
Spiral Matrix leetcode java的更多相关文章
- Spiral Matrix -- LeetCode
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- Search a 2D Matrix leetcode java
题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Spiral Matrix II leetcode java
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...
- [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 ...
随机推荐
- forof循环
一.语法 1.遍历数组 let myArr=[1,2,3,4,5]; for (let ele of myArr) { console.log(ele); } let myArr=[1,2,3,4,5 ...
- 【BZOJ-1913】signaling信号覆盖 极角排序 + 组合
1913: [Apio2010]signaling 信号覆盖 Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1232 Solved: 506[Subm ...
- 9、Redis处理过期keys的机制
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 1.Redis处理过期k ...
- js识别用户设备是移动端手机时跳转到手机网站
一.识别到用户的设备是手机等移动端设备时跳转到移动端网站 var mobileAgent = new Array("iphone", "ipod", " ...
- JSP Servlet学习笔记——使用fileupload上传文件
关键代码如下: index.jsp <body> <center> <h3>文件上传</h3> <font color="red&quo ...
- Javascript Array和String的互转换。
Array类可以如下定义: var aValues = new Array(); 如果预先知道数组的长度,可以用参数传递长度 var aValues = new Array(20); -------- ...
- systemtap 调试postgrel
http://blog.163.com/digoal@126/blog/static/16387704020137140265557/ dtrace http://blog.163.com/dig ...
- [Winform]Media Player播放控制面板控制,单击事件截获
摘要 在项目中有这样的一个需求,需要在一台宣传机器上循环播放一段视频,并在体验的用户单击鼠标左键的时候推出全屏,可以让用户体验电脑的其它功能. 解决方案 考虑到都是windows系统的,所以采用了wi ...
- 这里包含几乎所有的xcode正式版本
https://developer.apple.com/downloads/
- Windows Phone本地数据库(SQLCE):12、插入数据(翻译)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十二篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...