54. Spiral Matrix
题目:
Given a matrix of m x n elements (m rows, ncolumns), 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].
链接: http://leetcode.com/problems/spiral-matrix/
题解:
转圈打印矩阵,需要设置left, right, top和bot四个变量来控制, 注意每次增加行/列前要判断list所含元素是否小于矩阵的总元素数目。需要再想办法简化一下。
Time Complexity - O(mn), Space Complexity - O(1)
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix == null || matrix.length == 0)
return res;
int rowNum = matrix.length, colNum = matrix[0].length;
int left = 0, right = colNum - 1, top = 0, bot = rowNum - 1;
while(res.size() < rowNum * colNum) {
for(int col = left; col <= right; col++)
res.add(matrix[top][col]);
top++;
if(res.size() < rowNum * colNum) {
for(int row = top; row <= bot; row++)
res.add(matrix[row][right]);
right--;
}
if(res.size() < rowNum * colNum) {
for(int col = right; col >= left; col--)
res.add(matrix[bot][col]);
bot--;
}
if(res.size() < rowNum * colNum) {
for(int row = bot; row >= top; row--)
res.add(matrix[row][left]);
left++;
}
}
return res;
}
}
二刷:
跟一刷一样,设置上下左右四边界,然后编写就可以了。
Java:
Time Complexity - O(mn), Space Complexity - O(1)
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0) {
return res;
}
int rowNum = matrix.length, colNum = matrix[0].length;
int top = 0, bot = matrix.length - 1, left = 0, right = colNum - 1;
int elementsLeft = rowNum * colNum;
while (elementsLeft >= 0) {
if (elementsLeft >= 0) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
elementsLeft--;
}
top++;
}
if (elementsLeft >= 0) {
for (int i = top; i <= bot; i++) {
res.add(matrix[i][right]);
elementsLeft--;
}
right--;
}
if (elementsLeft >= 0) {
for (int i = right; i >= left; i--) {
res.add(matrix[bot][i]);
elementsLeft--;
}
bot--;
}
if (elementsLeft >= 0) {
for (int i = bot; i >= top; i--) {
res.add(matrix[i][left]);
elementsLeft--;
}
left++;
}
}
return res;
}
}
三刷:
条件是total > 0就可以了,不需要">="。
Java:
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0) return res;
int rowNum = matrix.length, colNum = matrix[0].length;
int left = 0, top = 0, right = colNum - 1, bot = rowNum - 1;
int total = rowNum * colNum;
while (total > 0) {
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
total--;
}
top++;
if (total > 0) {
for (int i = top; i <= bot; i++) {
res.add(matrix[i][right]);
total--;
}
right--;
}
if (total > 0) {
for (int i = right; i >= left; i--) {
res.add(matrix[bot][i]);
total--;
}
bot--;
}
if (total > 0) {
for (int i = bot; i >= top; i--) {
res.add(matrix[i][left]);
total--;
}
left++;
}
}
return res;
}
}
Reference:
https://leetcode.com/discuss/12228/super-simple-and-easy-to-understand-solution
https://leetcode.com/discuss/62196/ac-python-32ms-solution
https://leetcode.com/discuss/46523/1-liner-in-python
54. Spiral Matrix的更多相关文章
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- Leetcode 54. Spiral Matrix & 59. Spiral Matrix II
54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode OJ 54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- C# 获得手机归属地功能
今天通过查资料了解到web的页面抓取功能,应用HttpWebRequest和HttpWebResponse功能,从http://www.showji.com网站中抓取归属地信息 应该说这个方法是从别的 ...
- Png图片的透明部分穿透测试
private void Window_MouseMove(object sender, MouseEventArgs e){ NavBtnList.Clear(); Point mou ...
- Query execution was interrupted, max_statement_time exceeded
版本:5.6.16 群里看见一个问题,在备份的时候,报如下错误:[root@B28-19-75 bak]# mysqldump -root -p --single-transaction --mast ...
- php集成开发环境IDE
ZendStudio EclipsePHP PhpStorm NetBeans
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- nodejs小问题:express不是内部或外部命令(转载)
安装express之后发现居然提示express不是内部或外部命令. 工具/原料 Node.js安装包 方法/步骤 1 首先下载Node.js安装包,此处我用的是官方最新的v0.10.27 32位版: ...
- DB天气app冲刺二阶段第六天
今天干了一件让我有点小激动的事情 就是我感觉我貌似找到了为什么我的项目会闪退了有的时候..但是还不确定.等会会再试试看看到底对不对.好吧其实今天就干了这些事整整一下午调试,找bug,决定从头开始一点一 ...
- java集合类(四)About Set
接上篇:java集合类(三)About Iterator & Vector(Stack) 之前,在比较java常见集合类的时候,就了解到一点有关Set的特性.实现类及其要求等,读者可以去温习下 ...
- 高质量的javascript代码 -- 深入理解Javascript
一. 编写高质量的javascript代码基本要点a) 可维护的代码(Writing Maintainable Code)i. 可读(注释)ii. 一致(看上去是同一个人写的)iii. 已记录b) 最 ...
- 3142:[HNOI2013]数列 - BZOJ
题目描述 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨. 股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天 ...