当前位置:网站首页>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;
}
};
边栏推荐
- Misc of BUU (update from time to time)
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 云上有AI,让地球科学研究更省力
- Pallet management in SAP SD delivery process
- Simple use of JWT
- [advanced software testing step 1] basic knowledge of automated testing
- SQL Server Manager studio (SSMS) installation tutorial
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- BIO模型实现多人聊天
猜你喜欢
Database basics exercise part 2
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Apache DolphinScheduler源码分析(超详细)
Lesson 7 tensorflow realizes convolutional neural network
Fedora/REHL 安装 semanage
机器学习植物叶片识别
Pallet management in SAP SD delivery process
这个高颜值的开源第三方网易云音乐播放器你值得拥有
万丈高楼平地起,每个API皆根基
[unity] how to export FBX in untiy
随机推荐
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
Leetcode - 152 product maximum subarray
Simple query cost estimation
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
编译,连接 -- 笔记 -2
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
BIO模型实现多人聊天
Depth residual network
PCL实现选框裁剪点云
Every API has its foundation when a building rises from the ground
Apache DolphinScheduler源码分析(超详细)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
前缀和数组系列
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
[brush questions] how can we correctly meet the interview?