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

边栏推荐
- harbor api 2.0查询
- One interview question and one joint index every day
- HTC vive cosmos development - handle button event
- Brew install NVM command not found solution
- 【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
- Keywords implements and @override
- What is multimodal interaction?
- Photon pun refresh hall room list
- [UGV] schematic diagram of UGV version 32
- SSL universal domain name certificate
猜你喜欢

Learn about threads

Unity lens making

深度学习------不同方法实现Inception-10

力扣349. 两个数组的交集

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

【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model

harbor api 2.0查询

Unity3d lookat parameter description

【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter

Lambda&Stream
随机推荐
Encapsulating JDBC tool classes
What is SQL injection and how to avoid it?
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
圆心科技,很焦虑?
Software digital signature certificate
【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
On mask culling of unity
一条命令运行rancher
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
C # Foundation
Create a simple battle game with photon pun
Paging query, using jdbc-- paging query
Thread safety and processing caused by multithreading
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Serializable and Deserialize
What is multimodal interaction?
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
0 foundation starts self-study unit notes control direction becomes larger