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. JDBC操作数据库之修改数据

    使用JDBC修改数据库中的数据,起操作方法是和添加数据差不多的,只不过在修改数据的时候还要用到UPDATE语句来实现的,例如:把图书信息id为1的图书数量改为100,其sql语句是:update bo ...

  2. 克隆虚拟机 virtualbox 修改 uuid

    cmd E:\Program Files\Oracle\VirtualBox>VBoxManage.exe internalcommands sethduuid "E:\Program ...

  3. Python 接口测试(二)

    三:http状态码含义(来源于w3school): 状态码: 1xx: 信息 消息:          描述: 100 Continue   服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客 ...

  4. 【DDD】领域驱动设计实践 —— Application层实现

    本文是DDD框架实现讲解的第二篇,主要介绍了DDD的Application层的实现,详细讲解了service.assemble的职责和实现.文末附有github地址.相比于<领域驱动设计> ...

  5. oracle 表查询(一)

    通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salgrade表结构进行解说. emp 雇员表字段名称   数据类型       是否为空   备注-------- ...

  6. canvas,html2canvas等合成图片不清晰问题

    function  pxRa(cxt) { var backingStore = context.backingStorePixelRatio || context.webkitBackingStor ...

  7. C++ sizeof 误区 大公司面试题

    1.C++ 无成员变量和函数的类型的实例,求该实例的sizeof? 答:是1.(不是0) 2.如果在题1的基础上有1个成员变量,sizeof是(1+成员变量的大小)吗? 答:不是,是成员变量的大小. ...

  8. OpenJudge_1321:棋盘问题

    题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆 ...

  9. 坑中速记整理! 使用 kotlin 写第一个 ReactNative Android 模块

    Kotlin 和 Swift, 两大新宠! 借 ReactNative 熟悉下 kotlin 的用法,不料掉坑里面了.昨晚花了大半夜,趁这会儿思路清晰,把涉及到的一些关键信息,迅速整理下. 最佳的使用 ...

  10. 用$.getJSON() 和$.post()获取第三方数据做页面 ——惠品折页面(1)

    用$.getJSON() 和$.post()获取第三方数据做页面 首页 index.html 页面 需要jquery  和 template-web  js文件   可以直接在官网下载 中间导航条的固 ...