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

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

 public class Solution
{
public int[][] generateMatrix(int n)
{
int[][] res = new int[n][n]; int total = n*n;
int num = 1; int rowBegin = 0;
int rowEnd = n-1;
int colBegin = 0;
int colEnd = n-1; while(num <= total)
{
// traverse right (y changes)
for(int y=colBegin; y<=colEnd; y++)
res[rowBegin][y] = num++; rowBegin++; // move down one row // traverse down (x changes)
for(int x=rowBegin; x<=rowEnd; x++)
res[x][colEnd] = num++; colEnd--; // move left one column // traverse left (y changes)
for(int y=colEnd; y>=colBegin; y--)
res[rowEnd][y] = num++; rowEnd--; // move up one row // traverse up (x changes)
for(int x=rowEnd; x>=rowBegin; x--)
res[x][colBegin] = num++; colBegin++; // move right one column } return res;
}
}

参考资料:N/A

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

LeetCode 59. Spiral Matrix II (螺旋矩阵之二)的更多相关文章

  1. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  2. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  3. [leetcode]59. Spiral Matrix II螺旋遍历矩阵2

    Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral or ...

  4. [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 ...

  5. [Leetcode] spiral matrix ii 螺旋矩阵

    Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order. For ...

  6. LeetCode 54. Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  8. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  9. Leetcode59. Spiral Matrix II螺旋矩阵2

    给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...

随机推荐

  1. 从GO内存模型与调用协议理解defer closure的坑

    资料参考: 官网defer介绍: https://blog.golang.org/defer-panic-and-recover 深入解析go: 多值返回: https://tiancaiamao.g ...

  2. java 数组内的最大组合数

    给定一个任意长度的java数组,求数组内的数能组合出来的最大整数比如说{9,98,123,32} 最大就是 99832123 import java.util.Arrays; import java. ...

  3. Spring01-IOC

    1,新建一个Java Project 导入如下jar包和apache-commons-logging包 2, 在src目录下创建一个beans.xml的配置文件,先加入schema spring-be ...

  4. [UWP]分享一个基于HSV色轮的调色板应用

    1. 前言 上一篇文章介绍了HSV色轮,这次分享一个基于HSV色轮的调色板应用,应用地址:ColorfulBox - Microsoft Store 2. 功能 ColorfulBox是Adobe 色 ...

  5. 开源API集成测试工具 Hitchhiker v0.1.3 - 参数化请求

    Hitchhiker 是一款开源的 Restful Api 集成测试工具,你可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http://www.cnblogs.com/bro ...

  6. 一个“.java”文件中是否可以包含多个类(不是内部类)?有什么限制?

    可以,若这个类的修饰符是public则,其类名须与文件名相同.

  7. 每周分享之 二 http协议(3)

    本次分享http协议,共分为三部分,这是第三部分,主要讲解一个完整的http请求都经过哪些步骤,当我们在地址栏中输入网址,到返回页面都经历了什么 1.输入网址 当我们在浏览器中输入网址的时候,浏览器就 ...

  8. hdu 4123--Bob’s Race(树形DP+RMQ)

    题目链接 Problem Description Bob wants to hold a race to encourage people to do sports. He has got troub ...

  9. Docker入门之五数据管理

    在Docker使用过程中,需要对数据进行持久化或需要在多个容器之间进行数据共享,就会涉及容器的数据管理操作.主要有两种方式:1.数据卷 2.数据卷容器. 一.数据卷 数据卷是一个可供容器使用的特殊目录 ...

  10. libsvn_subr-1.so.0: undefined symbol: apr_atomic_xchgptr 故障解决

    源码编译安装完成之后,查看svn的安装版本会报以下错误 svn: symbol lookup error: /usr/local/subversion/lib/libsvn_subr-.so.: un ...