当前位置:网站首页>力扣59. 螺旋矩阵 II
力扣59. 螺旋矩阵 II
2022-06-30 04:45:00 【智慧的人不要秃头】
给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

方法:设定上下左右边界
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
//使用vector定义一个二维数组
vector<vector<int>> res(n, vector<int>(n, 0));
//定义上下左右所表示的行列,作为边界的控制因素
int top = 0;
int bottom = n - 1;
int left = 0;
int right = n - 1;
int num = 1; //放的第一个元素
int tar = n * n; //放的最后一个元素
//此循环用来保证所有的元素都被放入到矩阵中
//放置元素的顺序要遵循螺旋的规律,顺时针顺序
while(num <= tar) {
//数组上侧从左到右填充
//行不变,列变,具体是哪行,根据top(上侧)位置决定
for(int i = left; i <= right; ++i) {
res[top][i] = num++;
}
++top; //填充完,数组上侧已满一行,top+1
//数组右侧从上到下填充
//行变,列不变,具体是哪列,根据right(右侧)位置决定
for(int i = top; i <= bottom; ++i) {
res[i][right] = num++;
}
--right; //填充完,数组右侧已满一列
//数组下侧从右到左填充
//行不变,列变,具体是哪行,根据bottom(下侧)位置决定
for(int i = right; i >= left; --i) {
res[bottom][i] = num++;
}
--bottom; //填充完,数组下侧已满一行
//数组左侧从下到上填充
//行变,列不变,具体是哪列,根据left(左侧)位置决定
for(int i = bottom; i >= top; --i) {
res[i][left] = num++;
}
++left; //填充完,数组左侧已满一列
}
return res;
}
//时间复杂度:O(n^2),其中 n 是给定的正整数。矩阵的大小是 n×n,需要填入矩阵中的每个元素。
//空间复杂度:O(1)。除了返回的矩阵以外,空间复杂度是常数
};题解:
字丑莫怪,有不明白的点欢迎留言~~~~~~

边栏推荐
- A virtual reality secret room escape adventure, let you see Technology Singapore
- Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
- 破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
- My idea configuration
- Summary of the reasons why transactional doesn't work
- Learn about threads
- Error about the new version of UE4: unavigationsystemv1:: simplemovetoactor has been deprecated
- Cheap SSL certificate abroad
- Learning about signals
- Is the Flink connector JDBC open source? Where can I download it
猜你喜欢
![[UGV] schematic diagram of UGV version 32](/img/4b/03471d2cc96be5d57c97fd1c4e17dc.jpg)
[UGV] schematic diagram of UGV version 32

Oculus quest2 development: (I) basic environment construction and guide package

Efficiency test of adding and querying ArrayList and LinkedList

Qos(Quality of Service)

File and IO

What to do when the alicloud SSL certificate expires

Mongodb learning

Create a simple battle game with photon pun

Deep learning ----- different methods to realize inception-10

Process architecture and process management
随机推荐
Servlet lifecycle
力扣周赛293题解
One interview question every day to talk about the process of TCP connection and disconnection
Encapsulating JDBC tool classes
A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river
Mongodb learning
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
Dual domain SSL certificate
One interview question and one joint index every day
The role of break
How to repair expired SSL certificates?
Autowired注解警告的解决办法
Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
National Museum of Singapore - give you spiritual and physical satisfaction
BeanFactory创建流程
Singapore parent-child tour, these popular attractions must be arranged
Easyrecovery data recovery software recovers my photo and video data two years ago
Unity lens making