[算法][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 ... 
随机推荐
- RPC服务框架dubbo(一):简介和原理解析
			前置概念 在学习dubbo前,需要先了解SOA和RPC这两个概念. SOA 1.英文名称(Service Oriented Ambiguity) 2.中文名称:面向服务架构 2.1 有一个专门提供服务 ... 
- Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh
			国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心. svn版本 同样先示例server端的代码,基本步骤一样. 1.添加依 ... 
- nginx 反向代理 配置 https  实现http https同时存在 经测试 支持location 规则
			server { listen ssl; #监听443端口 server_name www.app01.com; ssl on; #启用ssl加密 ssl_certificate /etc/cert/ ... 
- CentOS下的强大的绘图工具 pinta
			[root@ok ~]# yum search pinta Loaded plugins: fastestmirror, refresh-packagekit, security Loading mi ... 
- 2、Reactive Extensions for .NET(译)
			实验3-引入 .net 中的 events 到 Rx 目标:前面实验中的使用各种工厂构造方法创建一个 可观察序列是一个部分.把 .net 中现有的异步数据源进行关联 是更重要的事情.在这次实验中我们将 ... 
- Oracle:shared memory realm does not exist
			1. 先描述一个连接Oracle 10g的错误:“shared memory realm does not exist” 如图所示Sqlplus连接时出现这个错误: 2. Oracle 服务器主要组件 ... 
- phoenix系统创建语句
			CREATE TABLE SYSTEM."CATALOG"( TENANT_ID VARCHAR NULL, TABLE_SCHEM VARCHAR NULL, TABLE_NAM ... 
- DLL编写中extern “C”和__stdcall的作用
			动态链接库的使用有两种方式,一种是显式调用.一种是隐式调用. (1) 显式调用:使用LoadLibrary载入动态链接库.使用GetProcAddress获取某函数地址. (2) ... 
- Javascript知识点:IIFE - 立即调用函数
			Immediately-invoked Function Expression(IIFE,立即调用函数),简单的理解就是定义完成函数之后立即执行.因此有时候也会被称为“自执行的匿名函数”(self-e ... 
- 深入理解JVM--JVM垃圾回收机制(转)
			Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ... 
