[算法][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 ...
随机推荐
- Spring Cloud(三):服务提供与调用
上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例. 案例中有三个角色:服务注册中心.服务提 ...
- mysql 主主复制(双主复制)binlog-do-db
[root@DB ~]# grep "binlog-do-db" /etc/my.cnf binlog-do-db = test [root@DB-S ~]# grep " ...
- 【计算机网络】IP分类
A类IP地址 A类IP地址:用可变的7位(bit)来标识网络号,可变的24位标识主机号,最前面一位为"0",即A类地址的第一段取值介于1-126之间.A类地址通常为大型网络而提供, ...
- MYSQL数据库的导出的几种方法
mysql的数据导出几种方法 从网上找到一些问题是关于如何从MySQL中导出数据,以便用在本地或其它的数据库系统之上:以及 将现有数据导入MySQL数据库中. 数据导出 数据导出主要有以下几种方法 ...
- 全局描述符表GDT
写在前面 添油加醋系列第二弹--剖析GDT 头文件:https://github.com/bajdcc/MiniOS/blob/master/include/gdt.h 实现:https://gith ...
- Windows操作系统下 使用c++ WIN32API禁用控制台最小化和关闭按钮
#include<Windows.h> //屏蔽控制台最小按钮和关闭按钮 HWND hwnd = GetConsoleWindow(); HMENU hmenu = GetSystemMe ...
- spoj687(后缀数组)
http://www.spoj.com/problems/REPEATS/ 题意:给一串字符,需要你求这一串字符中有连续重复的字符的重复次数....... 思路:这是和poj3693一种类型的题目.. ...
- JSON特殊字符的处理
本文转自:http://blog.csdn.net/btt2013/article/details/66970735 JSon 数据中的String 传递数据时,需要处理好特殊字符. JSon数据中有 ...
- StringJDBC更改数据库的两种方式
方法一jdbcTemplate.update("INSERT INTO USER VALUES(?, ?, ?, ?)", new Object[] {user.getId(), ...
- hdu 5384 Danganronpa(字典树)
题意: f(A,B)表示:B在A中作为子串出现的次数. 题目给出n个证据,m个子弹 Ai是证据.Bi是子弹.题目问:全部Bi对每一个Ai造成的伤害是多少,即每一个Bi在Ai中出现的次数总和. 解析: ...