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首选项SharedPreference-android学习之旅(六)

    SharedPrefenence采用的键值对的方式来进行存储,采用内部存储的方式. 实例 public class MainActivity extends Activity { private Sh ...

  2. printk的用法

    printk的用法 内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6> ...

  3. (NO.00005)iOS实现炸弹人游戏(五):游戏数据的初始化(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们现在来依次看一下上篇中提到的各个方法,首先介绍的是updat ...

  4. 程序员修炼之道中所有tips总结

    1         关心你的技艺 如果你不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2         思考!你的工作 关掉自动驾驶仪,接管操作.不断地批评和评估你的工作. 3    ...

  5. React Native之Navigator

    移动应用很少只包含一个页面.从你添加第二个页面开始,就得考虑如何管理多个页面间的跳转了. 导航器正是为此而生.它可以管理多个页面间的跳转,也包含了一些常见的过渡动画,包括水平翻页.垂直弹出等等. Na ...

  6. (六十九)使用block进行消息传递

    在两个类之间进行消息传递,一般通过代理或者block进行,代理写起来较为麻烦,block较为简单,但是block需要特别注意内存泄漏问题,注意self和block之间要为弱引用,下面介绍使用block ...

  7. Red Hat Enterprise Linux 5 64-bit chinese language support config steps

    Red Hat Enterprise Linux 5 64-bit 系统下安装中文语言支持方法 测试环境:Windows2012+Vmvare9.0+Red Hat Enterprise Linux ...

  8. URI记录

    URI:统一资源标识符(Uniform Resource Identifier,或URI)是一个用于标识某一互联网资源名称的字符串.该种标识允许用户对网络中(一般指万维网)的资源通过特定的协议进行交互 ...

  9. Let the Balloon Rise HDU 1004

    Let the Balloon Rise Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other ...

  10. C++ Primer 有感(复制控制)

    1.不管类是否定义了自己的析构函数,编译器都 自动执行类中非static数据成员的析构函数. 2.如果我们没有定义复制构造函数,编译器就会为我们合成一个.合成复制构造函数的行为是,执行逐个成员初始化, ...