当前位置:网站首页>[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
边栏推荐
- ABAP Dynamic Inner table Group cycle
- UltraEdit-32 温馨提示:右协会,取消 bak文件[通俗易懂]
- MySQL storage engine
- POJ培训计划2253_Frogger(最短/floyd)
- Construction of Hisilicon universal platform: color space conversion YUV2RGB
- Implementation steps of docker deploying mysql8
- Que savez - vous de la sérialisation et de l'anti - séquence?
- . Net interface can be implemented by default
- 机器人(自动化)课程的持续学习-2022-
- Summer 2022 daily question 1 (1)
猜你喜欢

一些常用软件相关
10 ways of interface data security assurance

cuda编程

【系统管理】清理任务栏的已删除程序的图标缓存

The most complete security certification of mongodb in history

ABAP dynamic inner table grouping cycle

Gpt-3 is a peer review online when it has been submitted for its own research

SQL injection -day15

tflite模型转换和量化

How to detect whether the MySQL code runs deadlock +binlog view
随机推荐
【安全攻防】序列化與反序列,你了解多少?
Restore backup data on GCS with tidb lightning
Opencv third party Library
The most complete deployment of mongodb in history
[development software] tilipa Developer Software
Kotlin Android environment construction
PHP 实现根据概率抽奖
POJ培训计划2253_Frogger(最短/floyd)
The most complete learning rate adjustment strategy in history LR_ scheduler
Create commonly used shortcut icons at the top of the ad interface (menu bar)
The first introduction of the most complete mongodb in history
Construction of Hisilicon universal platform: color space conversion YUV2RGB
API data interface of A-share index component data
复杂因子计算优化案例:深度不平衡、买卖压力指标、波动率计算
机器学习笔记 - 使用机器学习进行鸟类物种分类
史上最全学习率调整策略lr_scheduler
2022中青杯C题城市交通思路分析
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
Enter the rough outline of the URL question (continuously updated)
力扣------路径总和 III