当前位置:网站首页>[leetcode]Spiral Matrix II
[leetcode]Spiral Matrix II
2022-07-06 21:27:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
问题叙述性说明:
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 ]
]
基本思路:
本题是上一篇《Spiral Matrix》的变形。能够採用相同的遍历方法为其赋值。创建旋转矩阵。
代码:
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;
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116707.html原文链接:https://javaforall.cn
边栏推荐
- idea gradle lombok 报错集锦
- [leetcode] 700 and 701 (search and insert of binary search tree)
- qt-线程等01概念
- OSCP工具之一: dirsearch用法大全
- Class constant pool and runtime constant pool
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
- 自适应非欧表征广告检索系统AMCAD
- GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
- QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
- PHP lightweight Movie Video Search Player source code
猜你喜欢
Gpt-3 is a peer review online when it has been submitted for its own research
Calculation of time and space complexity (notes of runners)
Arduino droplet detection
QT thread and other 01 concepts
Web service performance monitoring scheme
ABAP 动态内表分组循环
Implementation steps of docker deploying mysql8
It's too convenient. You can complete the code release and approval by nailing it!
如何检测mysql代码运行是否出现死锁+binlog查看
Kotlin Android environment construction
随机推荐
机器学习笔记 - 使用机器学习进行鸟类物种分类
什么是 BA ?BA怎么样?BA和BI是什么关系?
Hisilicon 3559 universal platform construction: RTSP real-time playback support
[development software] tilipa Developer Software
The most complete learning rate adjustment strategy in history LR_ scheduler
Machine learning notes - bird species classification using machine learning
PIP download only, not install
Class constant pool and runtime constant pool
codeforces每日5题(均1700)-第七天
[leetcode] 700 and 701 (search and insert of binary search tree)
One of oscp tools: dirsearch usage Encyclopedia
手机号国际区号JSON格式另附PHP获取
Arduino droplet detection
【系统管理】清理任务栏的已删除程序的图标缓存
【安全攻防】序列化与反序列,你了解多少?
概率论公式
太方便了,钉钉上就可完成代码发布审批啦!
再AD 的 界面顶部(菜单栏)创建常用的快捷图标
Implementation of map and set
The true face of function pointer in single chip microcomputer and the operation of callback function