当前位置:网站首页>[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
边栏推荐
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- Create binary tree
- Event listener
- Gaussian processes of sklearn
- Understanding and installing MySQL
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
- Explain of MySQL optimization
- Practical operation - Nacos installation and configuration
- In depth source code analysis servlet first program
- Framework analysis 1 Introduction to system architecture
猜你喜欢
In depth source code analysis servlet first program
Create binary tree
Understanding and installing MySQL
Guaba and Computational Geometry
Addition, deletion, modification and query of MySQL table
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Pytorch learning record (7): skills in processing data and training models
Practical operation - Nacos installation and configuration
JDBC connection database
Pytoch -- data loading and processing
随机推荐
常用编程记录——parser = argparse.ArgumentParser()
图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration
Rainbow (DP)
MySQL best practices for creating tables
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Example of reentrant lock thread waiting to wake up
In depth understanding of the relationship between dncblevel and noise denoising in the paper
4. Print form
The bottom implementation principle of thread - static agent mode
Event listener
POJ - 2955 brackets interval DP
D. Optimal partition segment tree optimization DP
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
Formation à la programmation
Integration and induction of knowledge points of automatic control principle (Han min version)
3. Continuous integer
Filebrowser realizes private network disk
Advanced operation of idea debug
Collections multiple parameter sorting
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison