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

注意:是螺旋形,不是S形!

特点:每次运动时,要么行不变,要么列不变;

代码思路很好!!!

代码:

package leetcode;

import java.util.ArrayList;
import java.util.List;

public class CorrectSpiralMatrix {

public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        if (matrix == null || matrix.length < 1) {
            return list;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        if (m == 1 && n == 1) {
            list.add(matrix[0][0]);
            return list;
        }
        int dir = 0;
        int top = 0, right = n-1, left = 0, buttom = m-1;     //对应的行数,列数应该一致;

while (top <= buttom && left <= right) {              //0,1,2,3代表四个方向!
            if (dir == 0) {
                for (int i = left; i <= right; i++) {
                    list.add(matrix[top][i]);
                }
                top++;
            }
            if (dir == 1) {
                for (int i = top; i <= buttom; i++) {
                    list.add(matrix[i][right]);
                }
                right--;
            }

if (dir == 2) {
                for (int i = right; i >= left; i--) {
                    list.add(matrix[buttom][i]);
                }
                buttom--;
            }
            if (dir == 3) {
                for (int i = buttom; i >= top; i--) {
                    list.add(matrix[i][left]);
                }
                left++;
            }
            dir = (dir + 1) % 4;       //更改方向!
        }
        return list;
    }

public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[][] arr = { { 1, 2, 3 ,4,5,6}, { 7,8, 9,10,11,12 }, { 12,13,14,15,16,17 } };
        Long long1 = System.currentTimeMillis();
        System.out.print(spiralOrder(arr));
        Long long2 = System.currentTimeMillis();
        Long long3 = long2 - long1;
        System.out.println("耗时" + long3 + "毫秒!");
    }

}
教训:为什么不能用回溯?

时间: 2024-08-08 12:23:15

LeetCode--Spiral Matrix的相关文章

LeetCode:Spiral Matrix I II

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 [058]

[题目] 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]. [题意] 螺旋输出MxN矩阵 [注意

LeetCode: Spiral Matrix II [058]

[题目] 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,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven 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 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

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一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

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

LeetCode: Spiral Matrix 解题报告

Spiral MatrixGiven 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 1: 使

LeetCode——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]. 原题链接:https://oj.leetcod

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 ] ] 原题链接:https://oj.leetcode.com/problems/spiral-m

[Leetcode] 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]. 题意:以螺旋的方式,顺时针访问数组. 思路:按照遍