1. 原题链接

https://leetcode.com/problems/spiral-matrix/description/

2. 题目要求

给定一个二维整型数组,返回其螺旋顺序列表,例如:

最后返回结果为 [1,2,3,6,9,8,7,4,5]

3. 解题思路

按照螺旋的顺序进行遍历,每一次遍历螺旋顺序里的一个圈,如下图每一种颜色代表一次遍历得到的结果

4. 代码实现

import java.util.ArrayList;
import java.util.List; public class SpiralMatrix54 {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 0, 0}};
System.out.println(spiralOrder(matrix).toString());
} public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if (matrix.length == 0) return res;
int rowBegain = 0, rowEnd = matrix.length - 1; // 行数
int colBegain = 0, colEnd = matrix[0].length - 1; // 列数
while (rowBegain <= rowEnd && colBegain <= colEnd) { // 一次遍历一个螺旋顺序的圈
for (int i = colBegain; i <= colEnd; i++) {
res.add(matrix[rowBegain][i]);
}
rowBegain++; for (int i = rowBegain; i <= rowEnd; i++) {
res.add(matrix[i][colEnd]);
}
colEnd--; if (rowBegain <= rowEnd) {
for (int i = colEnd; i >= colBegain; i--)
res.add(matrix[rowEnd][i]);
}
rowEnd--; if (colBegain <= colEnd) {
for (int i = rowEnd; i >= rowBegain; i--)
res.add(matrix[i][colBegain]);
}
colBegain++;
} return res; }
}

  

LeetCode: 54. Spiral Matrix(Medium)的更多相关文章

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

  2. 【leetcode】Spiral Matrix(middle)

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

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

  4. Leetcode 54:Spiral Matrix 螺旋矩阵

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

  5. LeetCode - 54. Spiral Matrix

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

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

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

  7. Leetcode#867. Transpose Matrix(转置矩阵)

    题目描述 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:[[1 ...

  8. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

  9. [leetcode]54. Spiral Matrix二维数组螺旋取数

    import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n ...

随机推荐

  1. [POI2015]KIN

    题目 感觉这种题好套路啊,怎么又是这个做法 癌不过怎么没有人和我一样些写套路做法,那干脆来写个题解吧 我们考虑枚举区间的右端点,这样我们只需要考虑从\(1\)到\(i\)的最大区间就好了 由于我们选择 ...

  2. git branch 进入编辑状态

    命令行输入git branch,发现进入编辑状态,都要:wq,非常不方便,这样配置 git config --global core.pager ''

  3. 一张图解释 implicit

  4. python调用对象属性出错:AttributeError: 'function' object has no attribute '_name_'

    出错如下图所示: 原来是因为把__name__写成_name_, 下图为正确结果:

  5. java线程安全单例

    public class MySingleton { // 使用volatile关键字保其可见性 volatile private static MySingleton instance = null ...

  6. 点击HTML页面问号出现提示框

    本demo的功能:点击页面按钮在其边缘出现提示信息,点击页面任何一处则消失. 如下图: 1.所需插件: jquery插件: layer插件: 2.HTML内容: ==注意==: class=" ...

  7. oracle 12如何解锁账户锁定状态及修改忘记的密码

    有两种方法,大同小异吧,感觉命令真是个好东西,哈哈哈哈,挽救了我安了4次才安好的oracle!!! 方法一: 1.如果忘记密码,找到忘记密码的是哪个用户身份,如果用户被锁定,可以使用下面说的方法解除锁 ...

  8. python3爬虫-网易云排行榜,网易云歌手及作品

    import requests, re, json, os, time from fake_useragent import UserAgent from lxml import etree from ...

  9. windows用交互式命令执行python程序

    1.进入cmd命令 windows+r2.进入盘符,eg:E:3.使用dir命令查看当前文件夹下的所有目录4.使用绝对路径或者相对路径和cd命令直接进入想要到达的文件夹目录(或者使用cd命令一步一步达 ...

  10. C# 对象的深复制和浅复制

    2019年第一篇博客,好吧,又大了一岁了,继续加油吧. 正文: C# 中的对象,众所周知是引用类型,那么如何复制对象Object呢,我们来看看下面这段代码: public class User { p ...