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 ...
随机推荐
- Linux驱动之混杂设备(misc)
字符设备之混杂设备: 定义混杂设备: struct misdevice{ int minor; //为什么这里只有次设备号,因为混杂设备是一种在 /////////////////////////Li ...
- BZOJ.2456.mode(绝对众数)
题目链接 \(Description\) 限制空间(只能保留两个变量),求给定n个数中出现次数超过\(\frac{n}{2}\)的数. \(Solution\) 维护两个变量,\(now\)和\(cn ...
- Gitlab使用QQ企业邮箱发送邮件
注册QQ企业邮箱 地址 https://exmail.qq.com/signupfree?refer=intro#signup/free 注册完成后解析 编辑/etc/gitlab/gitlab.rb ...
- OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉
Challenge Your Template 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/G Description ACM ...
- POJ 1386 Play on Words (有向图欧拉路径判定)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8768 Accepted: 3065 Des ...
- STM32 F4 SPI Accelerometer
STM32 F4 SPI Accelerometer
- systemtap 调试postgrel
http://blog.163.com/digoal@126/blog/static/16387704020137140265557/ dtrace http://blog.163.com/dig ...
- Revit API得到类别Category设置类别可见性
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class c ...
- [Winform]检测exe是否已经运行,并将其置顶
摘要 在很多pc应用中,基本上都需要有这样的判断,保证在一个终端只运行一个winform的client.并且如果最小化了,用户再次双击桌面图标的时候,将client置顶显示. 解决方案 需要使用win ...
- C#写UTF8文件时指定是否含BOM头
BOM的基本概念 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE在UCS中是不存在的字符,所以不应该出现在实 ...