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

For example,
Given the following matrix:

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

You should return [1,2,3,6,9,8,7,4,5].

我们就用四个变量,向右移动增加colBegin,向下移动增加rowBegin,向左移动减小colEnd,向上移动减小rowEnd。
当向左或向上移动时,必须检查行或列是否仍然存在,以防止重复

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res=new ArrayList<>();
if(matrix==null||matrix.length==0) return res;
int rowBegin=0;//行开始
int rowEnd=matrix.length-1;
int colBegin=0;//列
int colEnd=matrix[0].length-1;
while(rowBegin<=rowEnd&&colBegin<=colEnd){
//向右遍历,遍历完rowBegin要加一,下移一行
for(int j=colBegin;j<=colEnd;j++)
res.add(matrix[rowBegin][j]);
rowBegin++;
//向下遍历
for(int i=rowBegin;i<=rowEnd;i++)
res.add(matrix[i][colEnd]);
//左移一列,准备向左遍历
colEnd--;
//向左遍历之前要保证这一行是存在的(没有被遍历过)
if(rowBegin<=rowEnd){
for(int j=colEnd;j>=colBegin;j--)
res.add(matrix[rowEnd][j]);
}
//上移一行
rowEnd--;
//向上遍历之前要保证这一列是存在的
if(colBegin<=colEnd){
for(int i=rowEnd;i>=rowBegin;i--)
res.add(matrix[i][colBegin]);
}
//右移一列
colBegin++;
}
return res;
}
}

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] Spiral Matrix 螺旋矩阵

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

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

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

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

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

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

  6. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

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

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

  8. Leetcode54. Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  9. [算法][LeetCode]Spiral Matrix——螺旋矩阵

    题目要求 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spir ...

随机推荐

  1. 在Android Studio 上安装Genymotion插件

    首先去官网http://www.genymotion.net/下载Genymotion并安装好. 其次打开AS的设置界面,找到Plugins插件目录 然后在搜索里搜Genymotion,然后点击Bro ...

  2. Ext JS 6开发实例(四) :调整主视图

    上文把主界面设置好,但是主视图因为界面的微调出现了显示问题,本文将把它调整好了. 打开app/view/main/Main.js,可以看到主视图是派生于标签面板(Ext.tab.Panel)的.在视图 ...

  3. dbms_lob使用之-基础

     在Oracle中,存储在LOB中数据称为LOB的值,如使用Select   对某一LOB字段进行选择,则返回的不是LOB的值,而是该LOB字段的定位器(可以理解为指向LOB值的指针).如执行如下 ...

  4. 从Eclipse插件中读取资源

    可以通过Eclipse里的OSGi的Bundle类,获取插件目录下的某个文件的输入流: 1. Bundle bundle = Platform.getBundle(Activator.PLUGIN_I ...

  5. iOS开发之四:常用控件--UIButton的使用

    在介绍UIButton的用法前,要先了解一下它的父类UIControl,UIControl是所有具有事件处理功能控件的父类. 而该类控件一般响应事件又有三种形式:基于触摸.基础值.基础编辑.控件的层次 ...

  6. xml的今生今世

    跟随小编学习的脚步,今天小编来简单总结一下xml的今生今世,xml百度百科对她这样诠释到:可扩展标记语言 (ExtensibleMarkup Language, XML),用于标记电子文件使其具有结构 ...

  7. eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样

    eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样 第一种说法: 还真的找到解决的方法了, ...

  8. 新手推荐:Hadoop安装教程_单机/伪分布式配置_Hadoop-2.7.1/Ubuntu14.04

    下述教程本人在最新版的-jre openjdk-7-jdk OpenJDK 默认的安装位置为: /usr/lib/jvm/java-7-openjdk-amd64 (32位系统则是 /usr/lib/ ...

  9. Socket编程实践(4) --多进程并发server

    1.Socket地址复用 int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); in ...

  10. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...