题目描述

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

解题思路

LeetCode54.螺旋矩阵 的思想差不多,定义左上角的行索引,然后依次从左至右、从上至下、从右至左、从下至上遍历,注意起始索引和终止索引。

代码

 class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n, ));
int idx = , num = ;
while(idx * < n){
for(int col = idx; col < n - idx; col++)
res[idx][col] = num++;
for(int row = idx + ; row < n - idx; row++)
res[row][n - idx - ] = num++;
for(int col = n - idx - ; col >= idx; col--)
res[n - idx - ][col] = num++;
for(int row = n - idx - ; row > idx; row--)
res[row][idx] = num++;
idx++;
}
return res;
}
};

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

  1. LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵

    题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...

  2. Java实现 LeetCode 59 螺旋矩阵 II

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

  3. 【leetcode刷题笔记】Spiral Matrix II

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

  4. [Swift]LeetCode59. 螺旋矩阵 II | Spiral Matrix II

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

  5. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

  6. [Swift]LeetCode885. 螺旋矩阵 III | Spiral Matrix III

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  7. [Leetcode]59.螺旋矩阵Ⅱ

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

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

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

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

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

随机推荐

  1. oa_mvc_easyui_分页(4)

    1.数据层的编写 NewListInfoDal.cs: GetPageEntityList方法,根据start,end取出数据 --row_number() over()函数查询 LoadEntity ...

  2. Linux cat命令详解(连接文件并打印到标准输出设备上)

    cat:连接文件并打印到标准输出设备上 一.命令格式: cat [-AbeEnstTuv] [--help] [--version] filename 二.参数说明: -n 或 --number:由 ...

  3. 访问接口错误,com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng-item-service

    com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng ...

  4. linux命令详解——lsof

    lsof全名list opened files,也就是列举系统中已经被打开的文件.我们都知道,linux环境中,任何事物都是文件, 设备是文件,目录是文件,甚至sockets也是文件.所以,用好lso ...

  5. mongo启动报错问题处理

    关键错误信息child process failed, exited with error number 100 这是服务器断电导致数据库意外关闭导致的问题,处理方法也比较简单 rm -rf /var ...

  6. maven学习之路三

    我们在写代码的时候,有些项目会有重复代码,或者是重复项目结构,这样我们就可以用maven 生成一个项目的基本骨架,就像我之前介绍的哪个logindemo一样继承了webApp-achetype一样.我 ...

  7. xss技巧记录

    1.iframe的srcdoc属性 先演示一下 <iframe srcdoc="<script>alert(1)</script>"> 浏览器渲 ...

  8. spring-data-neo4j 4.2.4release文档概要

    Neo4j是一种开源的NoSQL图数据库,将数据以图(把一个个实体当作节点,连接节点的边表示节点间的关系)的形式保存,Neo4j也支持ACID事务管理.关系型数据库数据访问采用的是ORM(对象关系映射 ...

  9. k8s master节点添加kubectl的使用

  10. noi.ac NA534 【猫】

    一眼暴力DP 再一眼决策单调性? 打个表以为是四边形不等式?? 最后发现是斜率优化??? 于是成功写了个假斜率优化真四边形不等式拿了\(80\) 设\(f[i][j]\)表示有\(i\)个工作人员出发 ...