题目:

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

思路:

本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵。同理,我们也是逐个环的填充,每个环顺时针逐条边填充

/**
* @param {number} n
* @return {number[][]}
*/
var generateMatrix = function(n) {
if(n==0){
return [];
} var matrix=[];
for(var j=0;j<n;j++){
matrix[j]=[];
}
var circle=Math.ceil(n/2);
var a=n,val=1;
for(var i=0;i<circle;i++,a-=2){
for(var col=i;col<i+a;col++){
matrix[i][col]=val++;
}
for(var row=i+1;row<i+a;row++){
matrix[row][i+a-1]=val++;
}
for(var col=i+a-2;col>=i;col--){
matrix[i+a-1][col]=val++;
}
for(var row=i+a-2;row>i;row--){
matrix[row][i]=val++;
}
} return matrix;
};

【数组】Spiral Matrix II的更多相关文章

  1. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  2. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  3. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  6. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  7. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  8. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

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

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

  10. 【leetcode】Spiral Matrix II (middle)

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

随机推荐

  1. ansible facts 获取硬件信息

    facts 指的是 ansible_facts 变量,ansible 中使用 setup 模块来获取,包含系统的大部分基础硬件信息, [root@10_1_162_39 host_vars]# ll ...

  2. 大文件上传插件webupload插件

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  3. button 左边图片右边文字样式

        状态值 : 正常 状态值 : 选中   #pragma mark - buttonPress- (void)buttonPress:(UIButton * )sender {     if ( ...

  4. spring获取webapplicationcontext,applicationcontext几种方法详解(转)

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  5. Transaction And Lock--使用资源锁来控制并发

    写过程序的朋友都知道,在多线程处理时,对于非线程安全的对象,需用使用锁定特定对象(LOCK)的方法来保证串行操作.曾经有位开发询问我,在SQL Server内部是否有类似的实现方法来控制某一操作不能并 ...

  6. 使用ABP框架踩过的坑系列3

    从架构角度来讲,ApplicationService究竟应该如何定位,一种说法是直接对应用例UseCase, 也就是直接对应UI, 这个UI是广义的,不仅仅是浏览器的页面,也包括API调用.还是从我曾 ...

  7. C#线程运用基础

    ThreadStart ts=new ThreadStart(a.f);//ThreadStart 是一个委托,用以关联a.f方法Thread th=new Thread (ts);//Thread是 ...

  8. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  9. 程序媛计划——python数据库

    #实例:用数据库存储日记,实现日记本功能 #流程 #创建数据库 #coding:utf-8 import sqlite3 connect=sqlite3.connect('test.db') conn ...

  10. Elasticsearch分页

    Elasticsearch的数据都存在每个节点的分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.ElasticSearch的搜索请求一次请求最大量为10000.如果超过则会发生错误.那么, ...