LeetCode 59. 螺旋矩阵 II(Spiral Matrix II)
题目描述
给定一个正整数 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)的更多相关文章
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- Java实现 LeetCode 59 螺旋矩阵 II
59. 螺旋矩阵 II 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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 ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- [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 ...
- [Leetcode]59.螺旋矩阵Ⅱ
给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
随机推荐
- Git提交代码的小知识
1.需要切换到项目目录下并创建一个Repository用于提交代码到这个仓库里 cd /g/....//cd后面有空格,用于进入某个项目文件夹 git init//用于创建Repository 2.添 ...
- js文件的框架
Ext.define("BeidaSoft.SFJCGL.rcjwgl.bdgl.BdglGrid", { extend : "BeidaSoft.XTGL.base.Q ...
- 帝国cms 常用标签汇总
1.列表内容标签 [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 2.分页标签 [!--show. ...
- 前端点击下载excel表格数据
<el-button type="primary" @click="downloadChartData" size="mini"> ...
- TensorFlow入门——bazel编译(带GPU)
这一系列基本上是属于我自己进行到了那个步骤就做到那个步骤的 由于新装了GPU (GTX750ti)和CUDA9.0.CUDNN7.1版本的软件,所以希望TensorFlow能在GPU上运行,也算上补上 ...
- docker 入门(2)
1,多容器环境 运行docker容器 进入容器并查看该容器的IP exit退出容器 运行超小的linux的docker镜像alpine 可以看到如果没有提前把镜像pull到本地,直接run的话,它会自 ...
- 利用python3 调用zabbix接口完成批量加聚合图形(screens)
在上一篇博客中,我们完成的利用python3 调用zabbix接口批量增加主机,增加主机的item,增加主机的图形! 接下来我们完成批量增加主机的screen 首先我们要增加screen需要哪些参数呢 ...
- Delphi FileListBox组件
- kubernetes管理存储
一.Kubernetes 如何管理存储资源: 理解volume 首先我们学习 Volume,以及 Kubernetes 如何通过 Volume 为集群中的容器提供存储:然后我们会实践几种常用的 Vol ...
- 修正zen cart商品评论显示太短的问题
找到includes\modules\pages\product_reviews\header_php.php $reviews_query_raw = “SELECT r.reviews_id, l ...