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 ...
随机推荐
- 机器学习-SVM-手写识别问题
机器学习-SVM-手写识别问题 这里我们解决的还是之前用KNN曾经解决过的手写识别问题(https://www.cnblogs.com/jiading/p/11622019.html),但相比于KNN ...
- countUp.js-数字滚动效果(简单基础使用)
最近写了个移动端宣传页,里面有数字的效果,所以有使用到countUp.js. 以下内容有包括:h5页面countUp.js的引入和实例.参数说明.事件简单使用和描述.countUp.js源代码. 附上 ...
- 第十三篇 JS 操作table表格
JS 操作table表格 这节课难度可能高一点,因为没有提前解释if判断.for循环.这节课是直接把这两样用上了,老师先简单介绍一下: if,判断语句,判断就很简单了嘛,假如说1=1(1等于1),当然 ...
- Oracle VM VirtualBox 安装 Centos7 并配置静态IP
新建虚拟电脑 选择类型和版本 设置内存大小 现在创建虚拟硬盘 选择VDI 硬盘足够就选择 固定大小 选择虚拟硬盘存储位置不要放在默认的C盘下,设置大小为30G 等待创建虚拟硬盘 虚拟硬盘创建成果之后 ...
- nginx_rtmp
rtmp { server { listen ; chunk_size ; max_connections ; #音视频流上传和播放地址都是 rtmp://你的IP/live/streamName # ...
- c++ 实时通信系统(c++socket篇)
在上一篇简单的介绍了TCP/IP协议,在这一篇我们主要介绍socket的具体实现的函数 第一步首先我们套添加上头文件:(#pragma comment(lib, "WS2_32") ...
- 如何获取到app的包名
相信很多朋友在刚开始接触测试app的时候都不清楚app的包名是什么,接下来给大家介绍几种方法去获取. 一.手机设备已连接到电脑,点击进入app中,前提是电脑上装备了android-SDK,tools文 ...
- @EqualsAndHashCode
1.@Data注解包含了这些注解 * @see Getter * @see Setter * @see RequiredArgsConstructor * @see ToString * @see E ...
- HTTP与TCP的区别和联系(转)
https://www.cnblogs.com/baizhanshi/p/8482612.html
- K8S概念
1.master master是集群的网关和中枢,负责诸如为用户和客户端暴露api.跟踪其他服务器的健康状态.以最优方式调度工作负载,以及编排其他组件之间的通信等服务,它是用户或客户端与集群之间的核心 ...