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

思路:螺旋数组,需要控制输出方向,我的实现是定义一个boolean数组,与数组大小相同,数据初始全为false.然后定义一个int变量表示方向,碰到数据边界或者下一数据为true,则改变方向,直到越界或者全部为true。结束循环。

具体代码如下:

public class Solution {
    public List<Integer> spiralOrder(int[][] a) {
    	List<Integer> list = new ArrayList<Integer>();
    	if(a.length == 0 || a[0].length == 0){
    		return list;
    	}
        int i = 0;//行
        int j = 0;//列
        boolean[][] b = new boolean[a.length][a[0].length];

        int o = 0;//表示方向,0:右;1:下;2:左;3:上

        //在范围内循环,超出范围结束
        while(i < a.length && i >= 0 && j < a[0].length && j >= 0){
            if(b[i][j]){//如果已全部走完,结束循环
                break;
            }

            list.add(a[i][j]); //添加结果
            b[i][j] = true;//已添加的标记为true,表示已经添加

            switch(o){
                case 0://往右走的方向
                    if(j == a[0].length - 1 || b[i][j+1]){
                        o = 1;//走到最右或者已标记,方向往下走
                        i++;
                    }else{
                        j++;
                    }
                    break;

                case 1:
                    if(i == a.length - 1 || b[i+1][j]){
                        o = 2;//走到最下或者已标记,方向往左走
                        j--;
                    }else{
                        i++;
                    }
                    break;
                case 2:
                    if(j == 0 || b[i][j-1]){
                        o = 3;//走到最左或者已标记,方向往上走
                        i--;
                    }else{
                        j--;
                    }
                    break;
                case 3:
                    if(i == 0 || b[i-1][j]){
                        o = 0;//走到最上或者已标记,方向往右走
                        j++;
                    }else{
                        i--;
                    }
                    break;
            }
        }
        return list;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 07:02:20

leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法的相关文章

Leetcode 54:Spiral Matrix 螺旋矩阵

54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1

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]54. Spiral Matrix

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 54. Spiral Matrix &amp; 59. Spiral Matrix II

54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input:

[C++]LeetCode: 110 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 螺旋矩阵

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]54. Spiral Matrix二维数组螺旋取数

import java.util.ArrayList; import java.util.List; /** * 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

leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

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 previous ro

19.2.8 [LeetCode 54] Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,