[leedcode 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 ]
]
public class Solution {
    int res[][];
    int val=1;
    public int[][] generateMatrix(int n) {
        //主要是通过画图,判断每个分支的范围,本题还适应非方阵
         res=new int[n][n];
        int minRow=0;
        int maxRow=n-1;
        int minCol=0;
        int maxCol=n-1;
        int val=1;
        while(minRow<=maxRow&&minCol<=maxCol){
            generate(minRow,maxRow,minCol,maxCol);
            minRow++;
            maxRow--;
            minCol++;
            maxCol--;
        }
        return res;
    }
    public void generate(int minRow,int maxRow,int minCol,int maxCol){
        for(int i=minCol;i<=maxCol;i++){
            res[minRow][i]=val++;
        }
        for(int i=minRow+1;i<=maxRow;i++){
            res[i][maxCol]=val++;
        }
        for(int i=maxCol-1;i>=minCol;i--){
            res[maxRow][i]=val++;
        }
        for(int i=maxRow-1;i>minRow;i--){
            res[i][minCol]=val++;
        }

    }
}
时间: 2024-10-29 10:36:54

[leedcode 59] Spiral Matrix II的相关文章

[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 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:

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

[LeedCode OJ]#59 Spiral Matrix II

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/spiral-matrix-ii/ 题意: 给出一个n,返回一个n*n的螺旋矩阵 思路: 按照螺旋矩阵的特点,使用四个循环来模拟其行走的过程就可以了 class Solution { public: vector<vector<int> > generateMatrix(int n) { vector<

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 ] ] 和spiral matrix1很像,代码如下: p.p1 { margin: 0.0px 0.

59. Spiral Matrix II (Graph)

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 ] ] class Solution { public: vector<vector<int>

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

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

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 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu