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. D3——动态绑定数据

    一.绑定数组元素 , , , , ]; d3.select("body") .selectAll("p") .data(dataset) .enter() .a ...

  2. Oracle创建自增序列

    Oracle没有自增字段这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现. 先建一个测试表了: create table userlogin( id   number(6 ...

  3. BZOJ3670:[NOI2014]动物园(KMP)

    Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...

  4. 第三章.搭建MyBatis工程环境

    1.数据库的准备: 数据库: create DATABASE mybatis: 数据表: CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREM ...

  5. Linux磁盘分区,挂载

    分区基础知识 分区的方式:   1) mbr分区:     1.最多支持四个主分区     2.系统只能安装在主分区     3.扩展分区要占一个主分区     4.MBR最大只支持2TB,但拥有最好 ...

  6. NSLog的各种打印格式符 和 打印CGRect时用NSStringFromCGRect

    打印CGRect时用NSStringFromCGRect 转载自:http://blog.csdn.net/chenyong05314/article/details/8219270 1. 打印CG开 ...

  7. OC中对象的description方法

    周所周知,我们在做项目时, 可以在类的.m文件中重写该类的对象的描述description方法: 示例: -(NSString *)description{    NSString *str = [N ...

  8. FreeRTOS 查询任务 剩余的栈空间的 方法

    FreeRTOS 源码下载地址 1.官方文档提供了   函数  用来查询  任务 剩余   栈 空间,首先是看官方的文档解释(某位大神 翻译 的 官方文档.) 参数解释:     xTask:被查询任 ...

  9. careercup-扩展性和存储限制10.4

    题目 有一个数组,里面的数在1到N之间,N最大为32000.数组中可能有重复的元素(即有的元素 存在2份),你并不知道N是多少.给你4KB的内存,你怎么把数组中重复的元素打印出来. 解答 我们有4KB ...

  10. java模拟浏览器发送请求

    package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...