leetCode 59.Spiral Matrix II (螺旋矩阵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 ]

]

思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换。

具体代码如下:

public class Solution {
    public int[][] generateMatrix(int n) {

        int[][] a = new int[n][n];
        int o = 0;//定义方向,0:右;1:下;2:左,3:上
        int x = 0;//1-n2
        int i = 0;//行
        int j = 0;//列
        while(i >= 0 && i < n && j >= 0 && j < n){
        	if(a[i][j] > 0)
        		break;//已全部填满,结束
            a[i][j] = ++x;//填充数据
            switch(o){//很方向讨论情况
                case 0:
                    if(j == n-1 || a[i][j+1] > 0){
                        i++;
                        o = 1;
                    }else{
                        j++;
                    }
                    break;
                case 1:
                    if(i == n-1 || a[i+1][j] > 0){
                        j--;
                        o = 2;
                    }else{
                        i++;
                    }
                    break;
                case 2:
                    if(j == 0 || a[i][j-1] > 0){
                        i--;
                        o = 3;
                    }else{
                        j--;
                    }
                    break;
                case 3:
                    if(i == 0 || a[i-1][j] > 0){
                        j++;
                        o = 0;
                    }else{
                        i--;
                    }
                    break;
            }
        }
        return a;
    }
}

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

时间: 2024-10-27 14:20:57

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

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 思路:此题的意思是给一个为0或1的矩阵,求所有为1组成的最大矩阵的面积. 此题能够巧妙转化为求最大直方图面积的问题. public class Solution { //其思想是将每一列的1逐行相加,遇0为0.遇1相加 //然后转化为求每一行的最大直方图面积的求解

[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 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 ] ] 题目标签:Array 这道题目和之前的螺旋矩阵几乎没有区别,而且更简单.同样按照螺旋矩阵的特性

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的输入和输出反过来而已. 很简单,一圈一圈往里

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 OJ: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]. 典型的dfs问题,与前面一个问题比较像,代码如下

leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

leetCode 38.Count and Say (计数和发言) 解题思路和方法

Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" o