当前位置:网站首页>[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
边栏推荐
- Addition, deletion, modification and query of MySQL table
- JDBC tool class encapsulation
- Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
- Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
- Event listener
- LockSupport. Park and unpark, wait and notify
- Failure to deliver XID in Seata distributed transaction project
- container
- Contrôle automatique (version Han min)
猜你喜欢

深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索

SQL injection

Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code

Understanding and installing MySQL

Advanced operation of idea debug
![Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images](/img/9d/487c77b5d25d3e37fb629164c804e2.png)
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images

Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)

编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察

Chapter 4 of line generation - linear correlation of vector systems

Pytorch learning record (III): structure of neural network + using sequential and module to define the model
随机推荐
Techniques et principes de détection
2. Devops sonar installation
Generate excel template (drop-down selection, multi-level linkage)
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Doomsday (simple computational geometry)
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
H. Are You Safe? Convex hull naked problem
Algèbre linéaire chapitre 1 - déterminants
Graphic numpy array matrix
7.Domino piling
JDBC operation transaction
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
Use of multithreaded executors
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
20 excellent plug-ins recommended by idea
11.a==b?
自动控制(韩敏版)
Substring Inversion (Easy Version)
Customized communication between threads (reentrantlock)
Understanding and use of tp50, tp90 and tp99