Spiral Matrix leetcode java

题目

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

题解
这道题是实现题。

考虑2个初始条件,如果矩阵只有一行或者一列,那么无需转圈,依次输出即可。

其他情况均需转圈:从左到右,从上到下,从右到左,从下到上。 从大圈依次循环到小圈即可。

代码如下:

1    public ArrayList<Integer> spiralOrder(int[][] matrix) {
 2         ArrayList<Integer> result = new ArrayList<Integer>();
 3         if(matrix == null || matrix.length == 0)
 4             return result;
 5  
 6         int m = matrix.length;
 7         int n = matrix[0].length;
 8  
 9         int x=0; 
10         int y=0;
11  
12         while(m>0 && n>0){
13  
14             //if one row/column left, no circle can be formed
15             if(m==1){
16                 for(int i=0; i<n; i++){
17                     result.add(matrix[x][y++]);
18                 }
19                 break;
20             }else if(n==1){
21                 for(int i=0; i<m; i++){
22                     result.add(matrix[x++][y]);
23                 }
24                 break;
25             }
26  
27             //below, process a circle
28  
29             //top - move right
30             for(int i=0;i<n-1;i++)
31                 result.add(matrix[x][y++]);
32  
33             //right - move down
34             for(int i=0;i<m-1;i++)
35                 result.add(matrix[x++][y]);
36  
37             //bottom - move left
38             for(int i=0;i<n-1;i++)
39                 result.add(matrix[x][y--]);
40  
41             //left - move up
42             for(int i=0;i<m-1;i++)
43                 result.add(matrix[x--][y]);
44  
45             x++;
46             y++;
47             m=m-2;
48             n=n-2;
49         }
50  
51         return result;
52     }

Reference:http://www.programcreek.com/2013/01/leetcode-spiral-matrix-java/

Spiral Matrix leetcode java,布布扣,bubuko.com

时间: 2024-08-04 06:33:06

Spiral Matrix leetcode java的相关文章

Search a 2D Matrix leetcode java

题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previou

leetcode 59 Spiral Matrix II ------ java

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 ] ] 这道题其实就是第54题Spiral Matrix的输入和输出反过来而已. 很简单,一圈一圈往里

Spiral Matrix -- LeetCode

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]. 思路:规律:当我们沿水平方向走完一次(比如从左往

59. Spiral Matrix II java solutions

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 ] ] 1 public class Solution { 2 public int[][] gene

54. Spiral Matrix java solutions

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]. Subscribe to see which c

Spiral Matrix II leetcode java

题目: 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想法也是类似的,就是依照矩阵从外圈到内圈建立

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 example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

[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: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,则生成如下矩阵: