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. window.open和window.close的使用详解

    有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢?   我们知道window.open可以新 ...

  2. Android中各种onTouch事件

    Android里有两个类 android.view.GestureDetector android.view.GestureDetector.SimpleOnGestureListener 1) 新建 ...

  3. 如何保存JMeter的性能测试数据到ElasticSearch上,并且使用Kibana进行可视化分析(1)

    前言 Jmeter是一款性能测试,压力测试的开源工具,被大量的测试人员拿来测试产品的性能,负载等等. Jmeter除了强大的预置的各种插件,各种可视化图表工具以外,也有些固有的缺陷,例如: 我们往往只 ...

  4. shell输出加颜色

    shell输出加颜色 #cat a.sh #!/bin/sh blue=`tput setaf 4` reset=`tput sgr0` echo "${blue}[INFORMATION] ...

  5. poj 3258 River Hopscotch(二分搜索之最大化最小值)

    Description Every year the cows hold an ≤ L ≤ ,,,). Along the river between the starting and ending ...

  6. HTML--控制小人自由移动

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. JMeter脚本参数化和断言设置( CSV Data Set Config )

    用Badboy录制了Jmeter的脚本,用Jmeter打开后形成了原始的脚本.但是在实际应用中,为了增强脚本的多样性,就要使脚本参数化.这里我以登录为例,参数化用户账号与用户密码.  图1 :原始脚本 ...

  8. hdfs经常使用命令

    hadoop hdfs经常使用命令 hadoop fs -ls /user/deploy/recsys/workspace/ouyangyewei 查看ouyangyewei文件夹文件 hadoop ...

  9. ASE中的主要数据库

    Adaptive Server包括多种类型数据库: 必需数据库. “附加功能”数据库 .例子数据库 .应用数据库 1.必需数据库 master 数据库包含系统表,这些系统表中存储的数据被用来管理,有 ...

  10. 用SNMP协议实现系统信息监控--CentOS

    (1)  安装SNMP客户端以及服务端 安装内容为三项:net-snmp  net-snmp-devel   net-snmp-utils 安装命令:yum install net-snmp net- ...