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

Example 1:

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

Example 2:

Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) {
return res;
}
int rowBegin = 0, rowEnd = matrix.length - 1;
int colBegin = 0, colEnd = matrix[0].length - 1; while (rowBegin <= rowEnd && colBegin <= colEnd) {
for (int i = colBegin; i <= colEnd; i++) {
res.add(matrix[rowBegin][i]);
}
rowBegin += 1; for (int i = rowBegin; i <= rowEnd; i++) {
res.add(matrix[i][colEnd]);
}
colEnd -= 1; if (rowBegin <= rowEnd) {
for(int i = colEnd; i >= colBegin; i--) {
res.add(matrix[rowEnd][i]);
}
}
rowEnd -= 1; if (colBegin <= colEnd) {
for (int i = rowEnd; i >= rowBegin; i--) {
res.add(matrix[i][colBegin]);
}
}
colBegin += 1;
}
return res;
}
}

[LC] 54. Spiral Matrix的更多相关文章

  1. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  2. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. 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 ...

  5. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

  6. [array] leetcode - 54. Spiral Matrix - Medium

    leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...

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

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

  8. 54. Spiral Matrix以螺旋顺序输出数组

    [抄题]: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  9. 54. Spiral Matrix

    题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...

随机推荐

  1. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  2. Android开发—错误记录1:W/System.err: java.net.ConnectException: Connection refused

    W/System.err: java.net.ConnectException: Connection refused 前台访问后台时,出现访问被拒绝情况:W/System.err: java.net ...

  3. Momentum

    11.6 Momentum 在 Section 11.4 中,我们提到,目标函数有关自变量的梯度代表了目标函数在自变量当前位置下降最快的方向.因此,梯度下降也叫作最陡下降(steepest desce ...

  4. python os.path.dirname() abspath()

    测试文件的名称 path_test.py 先确定文件目录 (my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py ...

  5. jenkins pipeline 之 deploy k8s 环境并发送邮件通知

    项目中有更新代码之后触发jenkins任务,部署好之后并发送邮件给发开人员 #!/usr/bin/env groovy Date date = new Date()def time = date.fo ...

  6. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:队列操作

    import tensorflow as tf #1. 创建队列,并操作里面的元素. q = tf.FIFOQueue(2, "int32") init = q.enqueue_m ...

  7. Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)

    题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...

  8. day49-线程-事件

    #1.Event里面有一个标志flag,当e = Event()刚创建的时候,flag=False,阻塞,这个时候,e.is_set()也是等于False,阻塞. #e.set()让flag变成非阻塞 ...

  9. Android开发学习4

    学习内容: 1.RecyclerView 2.WebView

  10. apache启动失败,提示80端口被占用

    首先检查80端口被什么程序占用,方法:cmd进DOS,输入netstat -ano 80端口被为4的进程占用,有两种可能:一种情况是本机中安装了sqlserver 2008,80端口被SqlServe ...