Question

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

Solution

这道题只有笨方法,就是老老实实地遍历输入的二维数组,得到结果。

但是在遍历时有个技巧,就是按照环来遍历。(x, y)为遍历点的坐标。我们发现,当遍历完一圈后,(x, y)又会回到起点。所以对于接下来里面一圈的遍历,只需x++, y++即可。

由于输入不一定是正方形,有可能最后剩的一圈是一行或一列,此时根据省下的行数/列数判断,另行处理。

 public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<Integer>();
if (matrix == null || matrix.length < 1)
return result;
int m = matrix.length, n = matrix[0].length, x = 0, y = 0;
while (m > 0 && n > 0) {
// If only one row left
if (m == 1) {
for (int i = 0; i < n; i++)
result.add(matrix[x][y++]);
break;
}
// If only one column left
if (n == 1) {
for (int i = 0; i < m; i++)
result.add(matrix[x++][y]);
break;
}
// Otherwise, we traverse in a circle
// Left to Right
for (int i = 0; i < n - 1; i++)
result.add(matrix[x][y++]);
// Up to Down
for (int i = 0; i < m - 1; i++)
result.add(matrix[x++][y]);
// Right to Left
for (int i = 0; i < n - 1; i++)
result.add(matrix[x][y--]);
// Down to Up
for (int i = 0; i < m - 1; i++)
result.add(matrix[x--][y]);
x++;
y++;
m -= 2;
n -= 2;
}
return result;
}
}

Spiral Matrix 解答的更多相关文章

  1. Spiral Matrix II 解答

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

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

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

  3. [LeetCode] Spiral Matrix 螺旋矩阵

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

  4. LeetCode - 54. Spiral Matrix

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

  5. 【leetcode】Spiral Matrix II

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

  6. 【leetcode】Spiral Matrix II (middle)

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

  7. 59. Spiral Matrix && Spiral Matrix II

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

  8. LeetCode:Spiral Matrix I II

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

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

随机推荐

  1. 【剑指offer】面试题40:数组中只出现一次的数字

    题目: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路: 因为只有两个只出现一次的数字,所以所有数字进行异或之后得到值res一定不是0.这样,res ...

  2. allVncClients

    VNC Viewer Free Edition 37  RealVNC Ltd.  15,367  Freeware  1021.58 KB VNC is client and server remo ...

  3. VS2012 中使用Emacs布局

    微软的反开源行为导致它不断的衰落,问题是还不反省. 下面这篇文章介绍了如何安装emacs布局的插件: http://marxistprogrammer.blog.163.com/blog/static ...

  4. c语言结构体数组引用

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }dbdd[]={{,,}, {,,} };//初始化 void m ...

  5. python3-day6(模块)

    一.OS模块 1.os.system('ls -l') #子shell运行,获取返回值,不是结果. 2.os.popen('ls -l').read() #获取结果. 二.sys模块 1.sys.ar ...

  6. C# 图结构操作

    仿造<<Java常用算法手册>>里面对的算法,使用C#实现了一遍. 理论知识我就不讲解了,在这本书里面已经写的非常完美! 代码如何下: using System; using ...

  7. Qt5官方demo解析集21——Extending QML - Adding Types Example

    本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,只是这个系列和我们之前的Chapt ...

  8. Dynamics CRM 常用 C# 方法集合

    Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...

  9. 把自己的程序打成jar包,让别人调用

     我们写程序的时候往往需要把自己的程序打包成jar包,给第三方调用.Eclipse让我们非常方便的可以导出jar包.但是当程序里需要用到res里的资源时,往往就会出现问题.因为统自动生成的R类如果被打 ...

  10. iOS程序的加载过程

    1.执行main函数2.执行UIApplicationMain函数1> 创建一个UIApplication对象(UIApplication是整个程序的象征)一个应用只有一个application ...