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. [转]How to Leak a Context: Handlers & Inner Classes

    Consider the following code: public class SampleActivity extends Activity { private final Handler mL ...

  2. Kill占用指定端口的进程的方法

    (1)查询占用指定端口进程的PID 打开cmd命令行,输入netstat -ano|findstr 8080(指定端口号) 最后一列即为占用该端口的进程的PID (2)KILL指定PID的进程 紧接着 ...

  3. UVa 1331 - Minimax Triangulation(区间DP + 计算几何)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 1.springboot:入门项目

    简介: Springboot 来简化spring应用开发,约定大于配置,去繁从简,just run 就可以创建一个独立的,产品应用 背景: J2EE笨重开发,繁多的配置,低下的开发效率,复杂的部署流程 ...

  5. javascript中数组的22种方法 (转载)

    前面的话 数组总共有22种方法,本文将其分为对象继承方法.数组转换方法.栈和队列方法.数组排序方法.数组拼接方法.创建子数组方法.数组删改方法.数组位置方法.数组归并方法和数组迭代方法共10类来进行详 ...

  6. ServletRequest的四个主要方法

    package com.yunqing.servlet; import javax.servlet.*; import java.io.IOException; import java.util.Ar ...

  7. mongodb副本集优先级设置

    在设置mongodb副本集时,Primary节点.second节点,仲裁节点,有可能资源配置(CPU或者内存)不均衡,所以要求某些节点不能成为Primary我们知道mongodb的设置:  除了仲裁节 ...

  8. Ant自动化打多渠道包,Android批量打包提速

    Eclipse用起来虽然方便,但是编译打包android项目还是比较慢,尤其将应用打包发布到各个渠道时,用Eclipse手动打包各种渠道包就有点不切实际了,这时候我们用到Ant帮我们自动编译打包了. ...

  9. C# 对WinForm应用程序的App.config的加密

    默认情况下,我们需要对App.config文件里的connectionStrings片断进行加密处理,ASP.NET IIS 注册工具 (Aspnet_regiis.exe)可以胜任这个工作,但这个工 ...

  10. 大数据框架-HDFS

    HDFS:分布式文件系统,运行文件通过网络在多台主机分享的文件系统,分块写入(128M),适用于一次写入多次查询,不支持并发写(只能一块一块写),小文件不合适. nameNode(主节点,单个): 保 ...