当前位置:网站首页>Force buckle 59 Spiral matrix II
Force buckle 59 Spiral matrix II
2022-06-30 04:50:00 【A wise man should not be bald】
Give you a positive integer n , Generate a include 1 To n2 All the elements , And the elements are arranged in a clockwise spiral order n x n square matrix matrix .

Method : Set the upper, lower, left and right boundaries
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
// Use vector Define a two-dimensional array
vector<vector<int>> res(n, vector<int>(n, 0));
// Define the rows and columns represented by the top, bottom, left and right , As the controlling factor of the boundary
int top = 0;
int bottom = n - 1;
int left = 0;
int right = n - 1;
int num = 1; // First element placed
int tar = n * n; // Put the last element
// This loop is used to ensure that all elements are put into the matrix
// The order in which the elements are placed follows the law of spirals , Clockwise order
while(num <= tar) {
// The upper side of the array is filled from left to right
// The line doesn't change , Column change , Which line is it , according to top( Upper side ) Location determines
for(int i = left; i <= right; ++i) {
res[top][i] = num++;
}
++top; // Fill up , The upper side of the array is full of one row ,top+1
// The right side of the array is filled from top to bottom
// Row change , The column does not change , Which column is it , according to right( On the right side ) Location determines
for(int i = top; i <= bottom; ++i) {
res[i][right] = num++;
}
--right; // Fill up , The right side of the array is full of a column
// The lower side of the array is filled from right to left
// The line doesn't change , Column change , Which line is it , according to bottom( Lower side ) Location determines
for(int i = right; i >= left; --i) {
res[bottom][i] = num++;
}
--bottom; // Fill up , The lower side of the array is full of one row
// The left side of the array is filled from bottom to top
// Row change , The column does not change , Which column is it , according to left( left ) Location determines
for(int i = bottom; i >= top; --i) {
res[i][left] = num++;
}
++left; // Fill up , The left side of the array is full
}
return res;
}
// Time complexity :O(n^2), among n Is a given positive integer . The size of the matrix is n×n, You need to fill in every element of the matrix .
// Spatial complexity :O(1). In addition to the returned matrix , Space complexity is a constant
};Answer key :
Don't be so ugly , Welcome to leave a message if you don't understand ~~~~~~

边栏推荐
- 【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
- On mask culling of unity
- Geotrustov wildcard
- Deeply understand the function calling process of C language
- Unreal 4 learning notes - set player birth point
- [UGV] schematic diagram of UGV version 32
- 為什麼win10開熱點後電腦沒有網絡?
- SCM learning notes: interrupt learning
- Bean创建流程 与 lazy-init 延迟加载机制原理
- Network layer protocol hardware
猜你喜欢

Redis implements SMS login function (I) traditional session login

SSL universal domain name certificate

How to renew an SSL certificate

Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)

Autowired注解警告的解决办法

力扣59. 螺旋矩阵 II

Malignant bug: 1252 of unit MySQL export

Learn about threads

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

Dual domain SSL certificate
随机推荐
一条命令运行rancher
My idea configuration
IO stream, byte stream read / write copy
Network high concurrency
Deep learning ----- different methods to realize inception-10
为什么win10开热点后电脑没有网络?
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
[fpga] implementation of IIC read / write EEPROM
On mask culling of unity
This connection is not a private connection this website may be pretending to steal your personal or financial information
Is the Flink connector JDBC open source? Where can I download it
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
Redis implements SMS login function (II) redis implements login function
How to repair expired SSL certificates?
Redis implements SMS login function (I) traditional session login
One interview question and one joint index every day
Unity3d lookat parameter description
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
0 basic unity course. Bricklaying