当前位置:网站首页>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 ~~~~~~

边栏推荐
- 力扣349. 两个数组的交集
- 【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
- Difference between TCP three handshakes and four waves and tcp/udp
- Connect to the database and run node JS running database shows that the database is missing
- [FPGA] IIC读写EEPROM 的实现
- Webots notes day 2
- Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
- PBR material: basic principle and simple fabrication
- Oculus quest2 development: (I) basic environment construction and guide package
- Check London attractions suitable for parents and children in winter vacation
猜你喜欢

Unity realizes rotation and Revolution

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

【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey

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

Issue SSL certificate with IP address

Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium

Singapore parent-child tour, these popular attractions must be arranged

Unity3d lookat parameter description
![[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example](/img/d7/7bf43437edb87b69cdc5ae858f44e1.jpg)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example

The most comprehensive summary notes of redis foundation + advanced project in history
随机推荐
Singapore must visit these scenic spots during the Spring Festival
PBR material: basic principle and simple fabrication
2021-03-16
Static keyword
一条命令运行rancher
Create a simple battle game with photon pun
National Museum of Singapore - give you spiritual and physical satisfaction
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Wildcard SSL certificate issuing time
IIS request SSL certificate
Some books you should not miss when you are new to the workplace
Introduction to some representations, neighbors and degrees of Graphs
[fpga] implementation of IIC read / write EEPROM
Unit screenshot saved on the phone
Redis implements SMS login function (I) traditional session login
Process architecture and process management
【Paper】2017_ Distributed control for high-speed trains movements
The most comprehensive summary notes of redis foundation + advanced project in history
圆心科技,很焦虑?
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter