当前位置:网站首页>力扣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)。除了返回的矩阵以外,空间复杂度是常数
};题解:
字丑莫怪,有不明白的点欢迎留言~~~~~~

边栏推荐
- [control] multi agent system summary. 4. control agreement.
- Beanfactory creation process
- HTC vive cosmos development - handle button event
- National Museum of Singapore - give you spiritual and physical satisfaction
- 力扣589:N 叉树的前序遍历
- MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
- Create a simple battle game with photon pun
- Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
- Royal Albert Hall, a popular landmark in London
- Easyrecovery data recovery software recovers my photo and video data two years ago
猜你喜欢

Connect to the database and run node JS running database shows that the database is missing

Unity is associated with vs. there is a compiler problem when opening

BeanFactory创建流程

為什麼win10開熱點後電腦沒有網絡?

Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor

Unreal 4 unavigationsystemv1 compilation error

【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV

【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator

Cheap SSL certificate abroad

On mask culling of unity
随机推荐
Keywords implements and @override
Implementation of one interview question one distributed lock every day
Bean创建流程 与 lazy-init 延迟加载机制原理
Unreal 4 learning notes - data storage using blueprints
力扣2049:统计最高分的节点数目
【Paper】2017_ Distributed control for high-speed trains movements
Unity lens making
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
My idea configuration
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
[control] multi agent system summary. 5. system consolidation.
Method of applying for code signing certificate by enterprise
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
Arrays class
What is multimodal interaction?
Have a heart beating Valentine's day in Singapore
Create transfer generation point
IIS request SSL certificate