当前位置:网站首页>[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文件[通俗易懂]
- The most complete learning rate adjustment strategy in history LR_ scheduler
- 链表面试常见题
- Quick completion guide of manipulator (10): accessible workspace
- map和set的实现
- 【mysql】mysql中行排序
- [development software] tilipa Developer Software
- 一些常用软件相关
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
猜你喜欢

Tflite model transformation and quantification

Quick completion guide of manipulator (10): accessible workspace

预处理——插值

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

Adaptive non European advertising retrieval system amcad

Collection of idea gradle Lombok errors

如何检测mysql代码运行是否出现死锁+binlog查看

idea gradle lombok 报错集锦

Implementation of map and set

The most complete security certification of mongodb in history
随机推荐
【安全攻防】序列化與反序列,你了解多少?
Index of MySQL
ABAP dynamic inner table grouping cycle
使用 Dumpling 备份 TiDB 集群数据到 GCS
你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
ABAP 动态内表分组循环
[development software] tilipa Developer Software
List interview common questions
预处理——插值
Force buckle ----- path sum III
【编码字体系列】OpenDyslexic字体
1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
Adaptive non European advertising retrieval system amcad
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
红米k40s root玩机笔记
The most complete security certification of mongodb in history
vim —- 自己主动的按钮indent该命令「建议收藏」
ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
本机mysql
【刷题记录】2. 两数相加