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. [原]Android打包之Eclipse打多渠道包

    Android自动打包流程详细图: 步骤一和步骤二参考:[原]Android打包之Eclipse打包 步骤三:编写build.xml <?xml version="1.0" ...

  2. whoami

    功能说明:显示当前登录的用户名,.

  3. Python中返回SQL字段名

    def ReturnInfo(self, avalue, akey): cursor = connection.cursor() if type(avalue) == int: Sql = " ...

  4. Yii中POS和GET并用范例

    页面 <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'add-form', 'enableAjaxValida ...

  5. 交叉熵Cross-Entropy

    1.交叉熵:用来描述通信中将一个概率分布的最优编码用到另一个概率分布的平均比特数 公式: 2.交叉熵是不对称的 3.交叉熵的作用是表达两个概率分布的差异性 设概率分布p(x)和q(x),两个概率分布差 ...

  6. linux下构建SVN

    1. 安装subversion#yum -y install subversion2. 安装好了之后 新建一个svn目录#mkdir /home/svn3. 新建两个版本仓库#svnadmin cre ...

  7. Winodws SNMP服务安装和配置(Windows 2003 & 2008 R2)

    简单网络管理协议SNMP服务起着代理的作用,它会收集可以向SNMP管理站或控制台报告的信息.您可以使用SNMP服务来收集数据,并且在整个公司网络范围内管理基于Windows Server 2003.M ...

  8. LeetCode28.实现strStr() JavaScript

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  9. Window系统Oracle 安装

    一:安装Oracle 数据库软件 1.先去官网下载所需文件:http://www.oracle.com/technetwork/database/enterprise-edition/download ...

  10. 04 关于oracle的锁的级别以及介绍

    关于oracle的锁的级别以及介绍 oracle造成锁表的情况: 一.查看锁的对象视图:select object_id,session_id,locked_mode from v$locked_ob ...