当前位置:网站首页>[leetcode]Spiral Matrix II
[leetcode]Spiral Matrix II
2022-07-07 04:06:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
A narrative statement of the problem :
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example, Given n = 3
,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
The basic idea :
This topic is the last one 《Spiral Matrix》 Deformation of . It can be assigned with the same traversal method . Create a rotation matrix .
Code :
vector<vector<int> > generateMatrix(int n) { //C++
vector<vector<int> >result;
if(n <=0 )
return result;
for(int i = 0; i < n; i++){
vector<int> tmp(n,0);
result.push_back(tmp);
}
int rowBegin = 0;
int rowEnd = n-1;
int colBegin = 0;
int colEnd = n-1;
int count = 1;
while(rowBegin <= rowEnd && colBegin <= colEnd){
//to right
for(int j = colBegin; j <= colEnd; j++)
result[rowBegin][j] =count++;
rowBegin++;
//to down
for(int j = rowBegin; j <= rowEnd; j++)
result[j][colEnd] = count++;
colEnd--;
//to left
if(rowBegin <= rowEnd){
for(int j = colEnd; j >= colBegin; j--)
result[rowEnd][j] = count++;
}
rowEnd--;
//to up
if(colBegin <= colEnd){
for(int j = rowEnd; j >= rowBegin; j--)
result[j][colBegin] = count++;
}
colBegin++;
}
return result;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116707.html Link to the original text :https://javaforall.cn
边栏推荐
- Redis source code learning (30), dictionary learning, dict.h
- 二叉搜索树的实现
- Class常量池与运行时常量池
- Arduino droplet detection
- 史上最全学习率调整策略lr_scheduler
- Summer 2022 daily question 1 (1)
- 自适应非欧表征广告检索系统AMCAD
- 超越Postman,新一代国产调试工具Apifox,用起来够优雅
- QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
- Implementation steps of docker deploying mysql8
猜你喜欢
【写给初发论文的人】撰写综述性科技论文常见问题
Preprocessing - interpolation
自适应非欧表征广告检索系统AMCAD
Construction of Hisilicon universal platform: color space conversion YUV2RGB
SQL injection -day15
数据的存储
维护万星开源向量数据库是什么体验
Some thoughts on cross end development of kbone and applet
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
运算放大器应用汇总1
随机推荐
手机号国际区号JSON格式另附PHP获取
Antd Comment 递归循环评论
Redis source code learning (31), dictionary learning, dict.c (1)
预处理——插值
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
杭州电 3711 Binary Number
[record of question brushing] 2 Add two numbers
Antd comment recursive loop comment
史上最全学习率调整策略lr_scheduler
Kbone与小程序跨端开发的一些思考
The first introduction of the most complete mongodb in history
Restore backup data on GCS with tidb lightning
史上最全MongoDB之部署篇
Allow public connections to local Ruby on Rails Development Server
史上最全MongoDB之Mongo Shell使用
One of oscp tools: dirsearch usage Encyclopedia
map和set的实现
2022 middle school Youth Cup mathematical modeling question B fertility policy research ideas under the background of open three children
Use dumping to back up tidb cluster data to GCS
ABAP Dynamic Inner table Group cycle