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 ...
随机推荐
- Android实现发短信与打电话的功能
//发短信 class SendMsgClickListener implements OnClickListener { public void onClick(View v) { //调用Andr ...
- 【笔记】WPF之模板控件应用
最近在捣鼓WPF的动画,想自定义一个控件模型来实现动画. 目标功能是这样:在WPF项目文件中创建一个自定义用户控件模型,该模型最外层是一个Grid,Grid布局为3行1列,第一列是一个图片按钮,第二列 ...
- ActiveMQ之Queue
Queue实现的是点到点模型,在以下的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue发送多个消息. 代码如下: public class QueueTest { /** * @ ...
- Jira 6.0.3 安装与破解
如果你还没有使用Jira做项目跟踪与管理,那就赶紧试用一下吧.下面教你一步一步安装Jira 6.0.3,以及如何破解试用版. 一. 安装准备 1. 去Jira官方网站下载http://www.at ...
- php中函数内使用static修饰变量
首先理解静态变量的区别,声明一个函数test() function num(){ $a = 0; echo $a; $a++; } num();num();num(); //输出000 functio ...
- 短小强悍的JavaScript异步调用库
对于博文 20行完成一个JavaScript模板引擎 的备受好评我感到很惊讶,并决定用此文章介绍使用我经常使用的另一个小巧实用的工具.我们知道,在浏览器中的 JavaScript 绝大部分的操作都是异 ...
- VBS基础篇 - 动态数组
VBS中的动态数组需要使用System.Collections.ArrayList '建立动态数组 Dim Arrl : Set Arrl = CreateObject("System.Co ...
- ios的UIImage的两种不同的图片加载方式 tom猫
在ios的UI交互设计时,对图片的处理是难免的:不同的处理方式会对内存有不同的影响: ********************************************************* ...
- easyui页面布局
html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- 无root权限安装python
http://lujialong.com/?p=150 pipe 安装第三方包 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip http://www.cn ...