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游戏开发之SurfaceView的使用-android学习之旅(五)

    SurfaceView和View的区别 View是在ui主线程中,直接响应用户的操作,以及任务的分发,但是任务比较复杂会出现阻塞. SurfaceView则不会出现这种问题,以为它直接从内存等取得图像 ...

  2. 音乐API

    博主在前几篇博客中介绍了小Q聊天机器人的源码及其包含的一些功能,并在应用市场上上线了一个版本,其中有一个功能是歌曲搜索,即输入格式为"歌曲#歌曲名#歌手"即可搜索出相应的歌曲并进行 ...

  3. 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree

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

  4. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

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

  5. 敏捷测试(7)--基于story的敏捷基础知识

    基于story的敏捷基础知识----迭代启动会.迭代回顾会 除需求讲解意外,需要所有团队成员参加的会议仅有两个,分别是"迭代启动会"和"迭代回顾会". (1)迭 ...

  6. 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景

    ============================================================================ 服务的生命周期: 一.采用start的方式开始 ...

  7. Android高级控件(五)——如何打造一个企业级应用对话列表,以QQ,微信为例

    Android高级控件(五)--如何打造一个企业级应用对话列表,以QQ,微信为例 看标题这么高大上,实际上,还是运用我么拿到listview去扩展,我们讲什么呢,就是研究一下QQ,微信的这种对话列表, ...

  8. [案例]某体育用品公司在零售领域BI的产品应用解决方案

    随着某体育用品公司集团经营规模的不断扩大,信息化的建设也在不断的深入,从POS系统到ERP系统,从MAIL系统到OA系统,整个集团的每项工作都与信息系统密不可分,可以说是行业内信息化建设的先导者.但是 ...

  9. 《java入门第一季》之面向对象多态面试题(多态收尾)

    /* 看程序写结果:先判断有没有问题,如果没有,写出结果 */ class A { public void show() { show2(); } public void show2() { Syst ...

  10. AngularJS进阶(二十六)实现分页操作

    JS实现分页操作 前言 项目开发过程中,进行查询操作时有可能会检索出大量的满足条件的查询结果.在一页中显示全部查询结果会降低用户的体验感,故需要实现分页显示效果.受前面"JS实现时间选择插件 ...