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 ...
随机推荐
- [HDU4348]To the moon(主席树+标记永久化)
学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put ...
- bzoj 2209 括号序列
反转操作 + 翻转操作 = 对称操作 因为上面三个操作都是自己的逆操作,所以我们只需要实现对称操作和反转操作,就可以搞定翻转操作. #include <cstdio> #include & ...
- Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分
C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...
- .Net 环境下C# 通过托管C++调用本地C++ Dll文件
综述 : 本文章介绍.Net 环境下C# 通过托管C++调用本地C++ Dll文件, 示例环境为:VS2010, .Net4.0, Win7. 具体事例为测试C++, C#, 及C#调用本地C++D ...
- Revit API过滤元素类别(FamilySymbol与FamilyInstance)
仅OfCategory()过滤的元素包含系统FamilySymbolOfClass(typeof(FamilyInstance))过滤出来文档中族实例. ; ; ; ...
- Property's synthesized getter follows Cocoa naming convention for returning
Property's synthesized getter follows Cocoa naming convention for returning. 今天早上在整理代码的时候发现了如上警告. ...
- C#程序中判断DEBUG和RELEASE状态
编辑 删除 习惯了用老方式(注释的方式)来对程序进行调试,不过昨天才发现这样调试存在很大的隐患:在工程发布的时候如果忘记把该注释的代码注释掉,而让这些调试信息随工程一起发布,如果是可见的调试信息倒好发 ...
- Windows下安装WebLogic
WebLogic安装结束 以下是进入MyEclipse启动配置WebLogic
- python笔记34-类里面的__str__ 和__unicode__作用
前言 最近学django,看到不少教程里面models.py里面建表,写一个类的时候,习惯上加个__str__ ,开始不太明白,简单的实践后才知道是为了美化类实例的打印内容. python3 里面用_ ...
- h5语音录制及上传(Java版语音聊天系统)
Since Chrome version 47, Voice Recording works only on HTTPS sites 目前基于webikit(谷歌之类的webikit)和Gecko(F ...