当前位置:网站首页>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;
}
};
边栏推荐
- 18.多级页表与快表
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
- idea控制台彩色日志
- Visitor tweets about how you can layout the metauniverse
- Simple use of JWT
- Huawei equipment configuration ospf-bgp linkage
- Every API has its foundation when a building rises from the ground
- What is the biggest problem that fresh e-commerce is difficult to do now
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- MPLS experiment
猜你喜欢
“无聊猿” BAYC 的内忧与外患
leetcode6109. 知道秘密的人数(中等,周赛)
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
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
hydra常用命令
Establishment and operation of cloud platform open source project environment
Missing monitoring: ZABBIX monitors the status of Eureka instance
Apache dolphin scheduler source code analysis (super detailed)
数据仓库建设思维导图
漏了监控:Zabbix对Eureka instance状态监控
随机推荐
接口自动化测试框架:Pytest+Allure+Excel
Latex文字加颜色的三种办法
[daily question] 729 My schedule I
Apache dolphin scheduler source code analysis (super detailed)
Raspberry pie serial port login and SSH login methods
Kubernetes cluster builds ZABBIX monitoring platform
BIO模型实现多人聊天
BUU的MISC(不定时更新)
WPF之MVVM
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
“无聊猿” BAYC 的内忧与外患
[hot100] 739. Température quotidienne
Short video, more and more boring?
Blue Bridge Cup zero Foundation National Championship - day 20
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
18.多级页表与快表
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
LeetCode Algorithm 2181. 合并零之间的节点
Arduino tutorial - Simon games