Spiral Matrix

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 ]
]
 1 public class Solution {
 2     public List<Integer> spiralOrder(int[][] matrix) {
 3         List<Integer> result = new ArrayList<>();
 4         if (matrix == null || matrix.length == 0) {
 5             return result;
 6         }
 7         int m = matrix.length;
 8         int n = matrix[0].length;
 9         if (m == 1) {
10             for (int i = 0; i < n; i++) {
11                 result.add(matrix[0][i]);
12             }
13             return result;
14         }
15         if (n == 1) {
16             for (int i = 0; i < m; i++) {
17                 result.add(matrix[i][0]);
18         }
19             return result;
20         }
21         int xStart = 0;
22         int yStart = 0;
23         while (n > 0 && m > 0) {
24             if (m == 1 && n == 1) {
25                 result.add(matrix[yStart][xStart]);
26                 return result;
27             }
28             if (m == 1 && n > 1) {
29                 for (int i = 0; i < n; i++) {
30                     result.add(matrix[yStart][xStart + i]);
31                 }
32                 return result;
33             }
34             if (n == 1 && m > 1) {
35                 for (int i = 0; i < m; i++) {
36                     result.add(matrix[yStart + i][xStart]);
37                 }
38                 return result;
39             }
40             for (int i = 0; i < n - 1; i++) {
41                 result.add(matrix[yStart][xStart + i]);
42             }
43             for (int i = 0; i < m - 1; i++) {
44                 result.add(matrix[yStart + i][xStart + n - 1]);
45             }
46             for (int i = 0; i < n - 1; i++) {
47                 result.add(matrix[yStart + m - 1][xStart + n - 1 - i]);
48             }
49             for (int i = 0; i < m - 1; i++) {
50                 result.add(matrix[yStart + m - 1 - i][xStart]);
51             }
52             ++xStart;
53             ++yStart;
54             n = n - 2;
55             m = m - 2;
56         }
57         return result;
58     }
59 }
时间: 2024-08-09 02:18:15

Spiral Matrix的相关文章

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 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]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

[LeetCode][Java] Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

[LeetCode]59.Spiral Matrix II

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [分析] 模拟 [代码] /**-------------------------

【leetcode】Spiral Matrix

Spiral Matrix 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]. 如果下一步会遇到访问

leetcode Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

[LeetCode]Spiral Matrix 54

54.Spiral Matrix 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]. 好久没做题了,