# 题目描述

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 static List<Integer> spiralOrder(int[][] matrix) {

List<Integer> result = new ArrayList<Integer>();

if (matrix == null || matrix.length == 0) {
return result;
}

int startx = 0, endx = matrix.length - 1;
int starty = 0, endy = matrix[0].length - 1;

while (startx <= endx && starty <= endy) {

for (int y = starty; y <= endy; y++) {
}

for (int x = startx + 1; x <= endx; x++) {
}

if (startx == endx || starty == endy) {
break;
}

for (int y = endy - 1; y >= starty; y--) {
}

for (int x = endx - 1; x >= startx + 1; x--) {
}

startx++;
starty++;
endx--;
endy--;
}

return result;
}

#### 【LeetCode】54. Spiral Matrix 解题报告

2016-05-15 10:28:27

#### [Leetcode]54. Spiral Matrix @python

2016-01-18 21:27:37

#### [C++]LeetCode: 110 Spiral Matrix (螺旋输出矩阵元素)

2015-01-19 16:28:56

#### LeetCode（54）Spiral Matrix

2015-09-05 21:28:57

#### Spiral Matrix II -- LeetCode

2014-03-24 06:54:28

#### 【LeetCode with Python】 Spiral Matrix

2014-11-30 21:49:27

#### leetcode_c++： Spiral Matrix II（059）

2016-06-01 00:28:33

#### LeetCode54——Spiral Matrix

2015-10-24 21:39:23

#### 【LeetCode】59. Spiral Matrix II 解题报告

2016-05-15 11:21:35

#### 59. Spiral Matrix II Leetcode Python

2015-02-03 23:48:58

## 不良信息举报

LeetCode 054 Spiral Matrix