当前位置:网站首页>[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
边栏推荐
- HW notes (II)
- Restore backup data on GCS with br
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
- C task expansion method
- Food Chem | in depth learning accurately predicts food categories and nutritional components based on ingredient statements
- 数据的存储
- 2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析
- 超越Postman,新一代国产调试工具Apifox,用起来够优雅
- Kbone与小程序跨端开发的一些思考
猜你喜欢

leetcode:面试题 17.24. 子矩阵最大累加和(待研究)

你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?

Adaptive non European advertising retrieval system amcad

Do you choose pandas or SQL for the top 1 of data analysis in your mind?

【开发软件】 tilipa开发者软件

It's too convenient. You can complete the code release and approval by nailing it!

预处理——插值

【安全攻防】序列化與反序列,你了解多少?

【刷题记录】2. 两数相加

When QT uses qtooltip mouse to display text, the picture of the button will also be displayed and the prompt text style will be modified
随机推荐
Enter the rough outline of the URL question (continuously updated)
Ggplot facet detail adjustment summary
map和set的实现
[record of question brushing] 2 Add two numbers
Arduino droplet detection
Redis源码学习(30),字典学习,dict.h
链表面试常见题
红米k40s root玩机笔记
What is the experience of maintaining Wanxing open source vector database
Mysql-数据丢失,分析binlog日志文件
The most complete deployment of mongodb in history
Hongmi K40S root gameplay notes
使用切面实现记录操作日志
史上最全学习率调整策略lr_scheduler
[security attack and Defense] how much do you know about serialization and deserialization?
What is Ba? How about Ba? What is the relationship between Ba and Bi?
运算放大器应用汇总1
opencv第三方库
Implementation steps of docker deploying mysql8
[hcie TAC] question 3