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

  这道题目和之前的螺旋矩阵几乎没有区别,而且更简单。同样按照螺旋矩阵的特性,设置4个边界,rowBegin = 0, rowEnd = n-1, colBegin = 0, colEnd = n-1。

  螺旋矩阵的走法:num = 1; 每次把num++代入矩阵

    1.从rowBegin 这一行开始, 从左往右走 [colBegin ~ colEnd],走完把rowBegin++,这行已经走完不需要了。

    2.从colEnd 这一列开始,从上往下走 [rowBegin ~ rowEnd],走完把colEnd--,这列不需要了。

    3.从rowEnd 这一行开始,从右往左走 [colEnd ~ colBegin],走完把rowEnd--, 这行不需要了。

    4.从colBegin 这一列开始,从下往上走 [rowEnd ~ rowBegin],走完把colBegin++,这列不需要了。

相比与螺旋矩阵之1, 这题的矩阵都是n*n,所以不需要在每一个for loop 里设置额外条件,因为螺旋矩阵1 里面给的是 m*n 可能不是正方形。

Java Solution:

Runtime beats 57.87%

完成日期:07/20/2017

关键词:Array

关键点:螺旋矩阵的遍历模式;设置四个边界值

 1 public class Solution
 2 {
 3     public int[][] generateMatrix(int n)
 4     {
 5         int[][] res = new int[n][n];
 6
 7         int total = n*n;
 8         int num = 1;
 9
10         int rowBegin = 0;
11         int rowEnd = n-1;
12         int colBegin = 0;
13         int colEnd = n-1;
14
15         while(num <= total)
16         {
17             // traverse right (y changes)
18             for(int y=colBegin; y<=colEnd; y++)
19                 res[rowBegin][y] = num++;
20
21             rowBegin++; // move down one row
22
23             // traverse down (x changes)
24             for(int x=rowBegin; x<=rowEnd; x++)
25                 res[x][colEnd] = num++;
26
27             colEnd--; // move left one column
28
29             // traverse left (y changes)
30             for(int y=colEnd; y>=colBegin; y--)
31                 res[rowEnd][y] = num++;
32
33             rowEnd--; // move up one row
34
35             // traverse up (x changes)
36             for(int x=rowEnd; x>=rowBegin; x--)
37                 res[x][colBegin] = num++;
38
39             colBegin++; // move right one column
40
41         }
42
43         return res;
44     }
45 }

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-12-23 01:26:57

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

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

[C++]LeetCode: 110 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 Matrix的模型和解法一样.要求我们将1~n^2的数字

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

leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

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: