当前位置:网站首页>[leetcode 59] spiral matrix II
[leetcode 59] spiral matrix II
2022-04-23 06:24:00 【Don't steal my energy】
Title Description
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 .
Example 1:
Input :n = 3
Output :[[1,2,3],[8,9,4],[7,6,5]]
Ideas
The idea of this problem is the same as that I wrote before Spiral matrix They have the same idea of solving problems , The reader can read it directly Spiral matrix How to solve the problem . The former is applicable to any matrix ( The number of rows and columns can not be equal ), This requires a square matrix ( The number of columns is equal ), It belongs to the special case of the former .
vector<vector<int>> generateMatrix(int n){
vector<vector<int>> ans(n,vector<int>(n,0));
int up=0,left=0,down=n-1,right=n-1;
int count=1;// Used to count
while(true){
// Left to right
for(int i=left;i<=right;i++)
ans[up][i]=count++;
if(++up > down)
break;
// Up and down
for(int i=up;i<=down;i++)
ans[i][right]=count++;
if(--right < left)
break;
// Right to left
for(int i=right;i>=left;i--)
ans[down][i]=count++;
if(--down < up)
break;
// Down to up
for(int i=down;i>=up;i--)
ans[i][left]=count++;
if(++left > right)
break;
}
return ans;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011715.html
边栏推荐
- Plane semi intersecting plate
- 线性代数第二章-矩阵及其运算
- SQL optimization best practices
- 檢測技術與原理
- C language file operation
- 自动控制原理知识点整合归纳(韩敏版)
- container
- Customized communication between threads (reentrantlock)
- Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
- 20 excellent plug-ins recommended by idea
猜你喜欢
Create binary tree
Integration and induction of knowledge points of automatic control principle (Han min version)
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
檢測技術與原理
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
IO multiplexing of 09 redis
随机推荐
POJ - 2955 brackets interval DP
Automatic control (Han min version)
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Algèbre linéaire chapitre 1 - déterminants
Example of ticket selling with reentrant lock
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
Common sense of thread pool
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Best practices for MySQL storage time
MySQL basic madness theory
Installation and usage skills of idea
Consistent hash algorithm used for redis cache load balancing
JDBC connection database
Calculation (enter the calculation formula to get the result)
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
RPC must know and know
Filebrowser realizes private network disk
MySQL occasional Caton