当前位置:网站首页>leetcode59. 螺旋矩阵 II(中等)
leetcode59. 螺旋矩阵 II(中等)
2022-07-06 06:55:00 【重you小垃】


思路:模拟
具体思路:如果遇到边缘或者遇到之前访问过的改变方向
class Solution {
public:
int xy[4][2] = {
0, 1, 1, 0, 0, -1, -1, 0};
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> ans(n, vector<int>(n));
vector<vector<int>> flag(n, vector<int>(n));
int x = 0, y = 0, index = 0;
for (int i = 1; i <= n * n; ++i) {
ans[x][y] = i;
flag[x][y] = 1;
int newx = x + xy[index][0], newy = y + xy[index][1];
if (newx < 0 || newx >= n || newy < 0 || newy >= n || flag[newx][newy]) {
index = (index + 1) % 4;
}
x = x + xy[index][0];
y = y + xy[index][1];
}
return ans;
}
};
边栏推荐
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- mysql的基础命令
- UDP攻击是什么意思?UDP攻击防范措施
- Introduction to ros2 installation and basic knowledge
- Machine learning plant leaf recognition
- Fast target recognition based on pytorch and fast RCNN
- RichView TRVStyle 模板样式的设置与使用
- Number of query fields
- 19. Actual memory management of segment page combination
猜你喜欢

《从0到1:CTFer成长之路》书籍配套题目(周更)

Monotonic stack
![[unity] how to export FBX in untiy](/img/03/b7937a1ac1a677f52616186fb85ab3.jpg)
[unity] how to export FBX in untiy

Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm

顶测分享:想转行,这些问题一定要考虑清楚!

What is the difference between int (1) and int (10)? Senior developers can't tell!

Cif10 actual combat (resnet18)

云上有AI,让地球科学研究更省力

Prefix and array series

Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
随机推荐
kubernetes集群搭建Zabbix监控平台
On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
ROS2安装及基础知识介绍
Number of query fields
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Short video, more and more boring?
Visitor tweets about how you can layout the metauniverse
Leetcode daily question (1997. first day where you have been in all the rooms)
SQL Server Manager studio (SSMS) installation tutorial
A brief introduction of reverseme in misc in the world of attack and defense
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
攻防世界 MISC中reverseMe简述
接口自动化测试框架:Pytest+Allure+Excel
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
雲上有AI,讓地球科學研究更省力
【每日一题】729. 我的日程安排表 I
BUU的MISC(不定时更新)
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
UWA Pipeline 2.2.1 版本更新说明