当前位置:网站首页>[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
边栏推荐
- cuda编程
- MySQL的存储引擎
- Web service performance monitoring scheme
- The JSON format of the international area code of the mobile phone number is obtained with PHP
- Top 50 hit industry in the first half of 2022
- 【编码字体系列】OpenDyslexic字体
- tflite模型转换和量化
- 你心目中的数据分析 Top 1 选 Pandas 还是选 SQL?
- 学习使用js把两个对象合并成一个对象的方法Object.assign()
- 20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
猜你喜欢

【knife-4j 快速搭建swagger】
![[security attack and Defense] how much do you know about serialization and deserialization?](/img/1c/e5ae74e65bacf688d7f61cc1b71d3e.png)
[security attack and Defense] how much do you know about serialization and deserialization?

GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议

21. (article ArcGIS API for JS) ArcGIS API for JS rectangular acquisition (sketchviewmodel)

卡尔曼滤波-1

My brave way to line -- elaborate on what happens when the browser enters the URL

什么是 BA ?BA怎么样?BA和BI是什么关系?

Machine learning notes - bird species classification using machine learning

Free PHP online decryption tool source code v1.2

On file uploading of network security
随机推荐
机械臂速成小指南(十):可达工作空间
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
What is Ba? How about Ba? What is the relationship between Ba and Bi?
Kotlin Android 环境搭建
Vernacular high concurrency (2)
1200.Minimum Absolute Difference
Antd Comment 递归循环评论
ABAP Dynamic Inner table Group cycle
codeforces每日5题(均1700)-第七天
22. (ArcGIS API for JS) ArcGIS API for JS Circle Collection (sketchviewmodel)
海思3559万能平台搭建:RTSP实时播放的支持
Preprocessing - interpolation
自适应非欧表征广告检索系统AMCAD
Top 50 hit industry in the first half of 2022
力扣------路径总和 III
预处理——插值
PHP lightweight Movie Video Search Player source code
map和set的实现
It's too convenient. You can complete the code release and approval by nailing it!
Probability formula