[算法][LeetCode]Spiral Matrix——螺旋矩阵
题目要求
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]
.
分析
Java代码
public static ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> order = new ArrayList<Integer>();
if (matrix.length == 0 || matrix[0].length == 0) return order; int xMin = 0;
int yMin = 0;
int xMax = matrix[0].length - 1;
int yMax = matrix.length - 1; order.add(matrix[0][0]); int i = 0, j = 0;
while (true) {
while (i < xMax) order.add(matrix[j][++i]);
if (++yMin > yMax) break; while (j < yMax) order.add(matrix[++j][i]);
if (xMin > --xMax) break; while (i > xMin) order.add(matrix[j][--i]);
if (yMin > --yMax) break; while (j > yMin) order.add(matrix[--j][i]);
if (++xMin > xMax) break;
}
return order;
}
[算法][LeetCode]Spiral Matrix——螺旋矩阵的更多相关文章
- [LeetCode] 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 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- PAT甲级——1105 Spiral Matrix (螺旋矩阵)
此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- [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 ...
- 第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...
- spiral matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
随机推荐
- Android 使用PLDroidPlayer播放网络视频 依据视频角度自己主动旋转
近期由于项目需求 .须要播放网络视频 .于是乎 研究了一番 ,说说我遇到的那些坑 如今市面上有几个比較主流好用的第三方框架 Vitamio ( 体积比較大,有商业化风险 github:https:// ...
- Qt 2D绘图高级篇
1.拖动模式 在QGraphicView中提供了三种拖动模式,分别是: QGraphicsView::NoDrag :忽略鼠标事件,不可以拖动. QGraphicsView::ScrollHandDr ...
- Bitmap OutOfMemoryError
在创建Bitmap的时候因为对象过多而没有即使回收,导致的内存不足: java.lang.OutOfMemoryError E/AndroidRuntime(21898): at androi ...
- MySql图解给表添加外键
关于外键约束的几种方式,请移步鄙人的另外一个博客中的博文 http://blog.csdn.net/hadues/article/details/52558184
- atitit. 文件上传带进度条 atiUP 设计 java c# php
atitit. 文件上传带进度条 atiUP 设计 java c# php 1. 设计要求 1 2. 原理and 架构 1 3. ui 2 4. spring mvc 2 5. springMVC.x ...
- Atitit.数据库新特性战略规划 mssql sql server 2008 SQL2012 SQL2014
Atitit.数据库新特性 mssql sql server 2008 SQL2012 SQL2014 1. Sql2012 新特性 1 1.1. 增加了Sequence对象. 1 1.2. 新的分页 ...
- CEffectMgr类
#ifndef __EFFECTMGR_H__ #define __EFFECTMGR_H__ #include "GameFrameHead.h" namespace cocos ...
- JS四种方法去除字符串最后的逗号
<script> window.onload=function() { var obj = {name: "xxx", age: 30, sex: "fema ...
- locatin
//location对象 //初始url:http://www.wrox.com/WileyCDA: //将URL修改为:http://www.wrox.com/WileyCDA/#section1 ...
- Bus error (core dumped) 我重启了下superviser 资源cpu占用高
python policy.py Bus error (core dumped) 我重启了下superviser