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

边栏推荐
- Oculus quest2 development: (I) basic environment construction and guide package
- Unit screenshot saved on the phone
- Collective system
- Qos(Quality of Service)
- 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
- Implementation of one interview question one distributed lock every day
- MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
- Connect to the database and run node JS running database shows that the database is missing
- [UAV] kinematic analysis from single propeller to four rotor UAV
- 力扣589:N 叉树的前序遍历
猜你喜欢

Yolov5 torch installation

【Paper】2017_ Distributed control for high-speed trains movements

The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories

A virtual reality secret room escape adventure, let you see Technology Singapore

National Museum of Singapore - give you spiritual and physical satisfaction

Collective system

Learning about signals
![[UGV] schematic diagram of UGV version 32](/img/4b/03471d2cc96be5d57c97fd1c4e17dc.jpg)
[UGV] schematic diagram of UGV version 32

A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river

PS1 Contemporary Art Center, Museum of modern art, New York
随机推荐
圆心科技,很焦虑?
One interview question and one joint index every day
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
IO stream, buffer stream, automatic resource management
Cheap SSL certificate abroad
File system and directory operations
力扣292周赛题解
Webots learning notes
C # equipment synthesis
Marvel fan welfare: Singapore Madame Tussauds Wax Museum Marvel 4D experience Museum
SSL universal domain name certificate
What is an optocoupler circuit and what should be paid attention to in actual use?
Unreal 4 learning notes - data storage using blueprints
Transport layer protocol tcp/udp
How to renew an SSL certificate
What is multimodal interaction?
Qos(Quality of Service)
HTC vive cosmos development - handle button event
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
[control] multi agent system summary. 4. control agreement.