Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

题意:

按螺旋方式遍历矩阵。

Solution1: Simulation the process and implement it.

code

 class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix.length == 0 || matrix[0].length == 0) return res; int top = 0;
int bottom = matrix.length-1;
int left = 0;
int right = matrix[0].length-1; while(true){
for(int i = left; i <= right; i++) {
res.add(matrix[top][i]);
}
top++;
if(left > right || top > bottom) break; for(int i = top; i <= bottom; i++) {
res.add(matrix[i][right]);
}
right--;
if(left > right || top > bottom) break; for(int i = right; i >= left; i--) {
res.add(matrix[bottom][i]);
}
bottom--;
if(left > right || top > bottom) break; for(int i = bottom; i >= top; i--){
res.add(matrix[i][left]);
}
left++;
if(left > right || top > bottom) break;
}
return res;
}
}

[leetcode]54. Spiral Matrix螺旋矩阵的更多相关文章

  1. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  2. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  3. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  4. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  5. 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 ...

  6. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  7. 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 ...

  8. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. PAT甲级——1105 Spiral Matrix (螺旋矩阵)

    此文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90484058 1105 Spiral Matrix (25 分) ...

随机推荐

  1. 巧妇难为无米之炊( Model数据)

    一.相隔万里的客户端服务器数据交互 请求头发过去的轻量级文本数据,后台根据这些信息处理  response返回的如果时html的话,那么是全局刷新 在ajax中data回调获得了数据,然后操作dom进 ...

  2. day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁

    一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...

  3. Flask--(一对多demo)作者书籍模型

    一对多模型的增加和删除 后端实现: from flask import Flask from flask import flash from flask import redirect from fl ...

  4. Spring Boot使用单元测试

    一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...

  5. Pycharm初始创建项目和环境搭建

    Pycharm确实是一个非常不错的Python开发IDE,尤其对于初学者而言. 打开新建项目 1.选择新建一个Pure Python项目,新建项目路径可以在Location处选择. 2.Project ...

  6. jenkins 可以设置最多执行并发执行多少个

    系统-系统配置

  7. java中的静态变量、静态方法与静态代码块详解与初始化顺序

      我们知道类的生命周期分为装载.连接.初始化.使用和卸载的五个过程.其中静态代码在类的初始化阶段被初始化. 而非静态代码则在类的使用阶段(也就是实例化一个类的时候)才会被初始化. 静态变量 可以将静 ...

  8. 简谈OSI七层模型(网络层)

    七层模型,亦称OSI(Open System Interconnection)参考模型,是参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系. 它是一个七层的.抽象的模型 ...

  9. python程序正式开始

    第几个hello world 程序了,为曾经没有毅力的自己默哀下.今天的课程语言的分类,三大类:机器语言,汇编语言,高级语言. 其中最让我痛恨的就是汇编语言,我们大学没事开什么这课程,大学混日子的喔不 ...

  10. ORA-00911: invalid character 包含中文报错

    SQL在pl下正常执行在vs里报错ORA-00911: invalid character. 1.检查SQL末尾是否含有";" 去掉 2.sql包含中文报错 string sql ...