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

思路:设置四个边界变量。仅仅要不越界即可

public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> al = new ArrayList<Integer>();
if (matrix.length == 0)
return al;
int x1 = 0;
int y1 = 0;
int x2 = matrix.length - 1;
int y2 = matrix[0].length - 1;
while (x1 <= x2 && y1 <= y2) {
// up row
for (int i = y1; i <= y2; ++i)
al.add(matrix[x1][i]);
// right column
for (int i = x1 + 1; i <= x2; ++i)
al.add(matrix[i][y2]);
// bottom row
for (int i = y2 - 1; x2 != x1&& i >= y1; --i)
al.add(matrix[x2][i]);
// left column
for (int i = x2 - 1; y1 != y2 && i > x1; --i)
al.add(matrix[i][y1]); x1++;
y1++;
x2--;
y2--;
} return al;
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

LeetCode 53 Spiral Matrix的更多相关文章

  1. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  3. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  4. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

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

  6. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  7. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  8. LeetCode - 54. Spiral Matrix

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

  9. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

随机推荐

  1. Swift - 类的计算属性(使用get和set来间接获取/改变其他属性的值)

    1,Swift中类可以使用计算属性,即使用get和set来间接获取/改变其他属性的值,代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Calc ...

  2. linux下用tar命令将当前目录下文件按子目录压缩归档

    作者:iamlaosong 日常工作中,我们常常需要用tar命令将历史文件压缩归档,再删除源文件,以便节省空间.如果压缩归档成一个文件,就比较简单,用一条命令即可,如命令:tar -czf bak20 ...

  3. CentOS6.5与window远程桌面配置

    VNC配置手冊 一.服务端 VNC(Virtual Network Computing)是一种Linux系统(或者BSD.Mac等)下经常使用的图形化远程管理工具.使用的是RFB协议.VNC跟SSH一 ...

  4. cocos2d-x-lua基础系列教程四(lua多继承)

    lua - 多继承 1,模拟伪继承 新建extend_test.lua 新建extend_test.lua setmetable(a,b) b对象是a 对象的父类 a继承于b Account = { ...

  5. 打造你自己ajax上传图片

    今天,我们需要的图片上传插件,但是,互联网不提供符合他们的需要和易于使用的.所以我写了自己. 方法1,只使用jquery代码,.代码例如以下 <p> <label>上传图片&l ...

  6. 简单深刻:为控件创建MouseEnter和MouseLeave事件(覆盖WndProc,增加对消息的处理,真简单!)——连对CM_MOUSEENTER的消息处理都是颇有深意啊!

    其实很简单: unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, D ...

  7. easy_install MySQL-python

    python - Why can't easy_install find MySQLdb? - Stack Overflow easy_install MySQL-python

  8. map size mismatch; abort

    数据库启动时有莫名的提示map size mismatch; abort 原帖在这里:http://t.askmaclean.com/thread-1397-1-1.html 今天帮网友(centos ...

  9. Oracle定时执行存储过程(转)

    定时执行存储过程在平时开发中经常会用到,年前的时候自己也做了一个,由于时间关系一直没能记录,现记录下来.       首先用一个完整的例子来实现定时执行存储过程. 任务目标:每小时向test表中插入一 ...

  10. Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区

    Pyocr 0.2 发布,Python 的 OCR 库 - 开源中国社区 Pyocr 0.2 发布,Python 的 OCR 库