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. DMP文件的生成和使用

    1.生成dmp的程序 #include  <dbghelp.h> #pragma comment(lib,  "dbghelp.lib") //设置异常处理回调函数Se ...

  2. HDU 3549 Flow Problem(有向边网络流)

    九野的博客,转载请注明出处 :http://blog.csdn.net/acmmmm/article/details/11221561 题意:T个测试数据 下面n,m表示n个点m条有向带权边 m条边 ...

  3. iOS8指纹识别TouchID

    苹果在2014年6月3日的WWDC2014开幕式上推出了新版iOS8系统,界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好.iOS8通知中心更加强大,支持消息直接回复操作,并支持Quic ...

  4. FindWindowEx使用方法

    函数功能:该函数获得一个窗体的句柄,该窗体的类名和窗体名与给定的字符串相匹配.这个函数查找子窗体,从排在给定的子窗体后面的下一个子窗体開始.在查找时不区分大写和小写. 函数原型:HWND FindWi ...

  5. 百度GPSutil

    ================================================= package com.qcar.benz.biz.common; import com.aliba ...

  6. phprpc 使用实例(同时有Java、Android和Delphi客户端的例子)

    PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出重定向的.支持分级错误处理的.支持会话的.面向服务的高性能远程过 ...

  7. 浅谈Jquery的使用上篇

    一. 1.Jquery是什么?有什么特性? jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取.HTML 元素操作. CSS 操作 .HTML 事 ...

  8. 做一个牛XX的身份证号验证类(支持15位和18位)

    原文:做一个牛XX的身份证号验证类(支持15位和18位) #region 是否合法的中国身份证号码 protected bool IsChineseID() { if (str.Length == 1 ...

  9. Linux 利用hosts.deny 防止暴力破解ssh(转)

    一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...

  10. Ajax动态载入xml文件内容

    <%@page import="javax.swing.JOptionPane"%> <%@page import="com.ctl.util.*&qu ...