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

边栏推荐
- 2021-03-16
- [FPGA] IIC读写EEPROM 的实现
- Interprocess communication
- [control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
- SCM learning notes: interrupt learning
- Introduction to some representations, neighbors and degrees of Graphs
- 史上最全的Redis基础+进阶项目实战总结笔记
- Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
- Oculus quest2 development: (I) basic environment construction and guide package
- Easyrecovery data recovery software recovers my photo and video data two years ago
猜你喜欢

Interprocess communication

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

Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge

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

Deeply understand the function calling process of C language

【Paper】2019_ Distributed Cooperative Control of a High-speed Train

Wildcard SSL certificate issuing time

Lambda&Stream

Method of applying for code signing certificate by enterprise

为什么win10开热点后电脑没有网络?
随机推荐
JPA复合主键使用
IIS request SSL certificate
一条命令运行rancher
Draw on screen border in Commodore 64
Qos(Quality of Service)
National Museum of Singapore - give you spiritual and physical satisfaction
Redis implements SMS login function (II) redis implements login function
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
This connection is not a private connection this website may be pretending to steal your personal or financial information
SCM learning notes: interrupt learning
File and IO
Recommended cultural landmarks of these tourist attractions in Bangkok
Directory operations and virtual file systems
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
Why does the computer have no network after win10 is turned on?
力扣周赛293题解
Wildcard SSL certificate issuing time
Deep learning ----- different methods to realize inception-10
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories