当前位置:网站首页>Leetcode59. spiral matrix II (medium)
Leetcode59. spiral matrix II (medium)
2022-07-06 07:03:00 【Heavy garbage】
Ideas : simulation
Specific ideas : If you encounter an edge or a previously visited change of direction
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;
}
};
边栏推荐
猜你喜欢
kubernetes集群搭建Zabbix监控平台
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
前缀和数组系列
Prefix and array series
[brush questions] how can we correctly meet the interview?
呆错图床系统源码图片CDN加速与破解防盗链功能
微信公众号无限回调授权系统源码 全网首发
Cif10 actual combat (resnet18)
随机推荐
[some special grammars about C]
Wechat official account infinite callback authorization system source code, launched in the whole network
[daily question] 729 My schedule I
Latex文字加颜色的三种办法
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
Setting and using richview trvstyle template style
Pallet management in SAP SD delivery process
PCL realizes frame selection and clipping point cloud
雲上有AI,讓地球科學研究更省力
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
How to reconstruct the class explosion caused by m*n strategies?
GET 和 POST 请求类型的区别
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Huawei equipment configuration ospf-bgp linkage
How to find a medical software testing institution? First flight software evaluation is an expert
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
编译,连接 -- 笔记 -2
TS Basics
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
L'Ia dans les nuages rend la recherche géoscientifique plus facile