当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

接口自动化测试框架:Pytest+Allure+Excel

数据仓库建设思维导图

Fast target recognition based on pytorch and fast RCNN

AI on the cloud makes earth science research easier

leetcode59. 螺旋矩阵 II(中等)

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

Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案

Visitor tweets about how you can layout the metauniverse

leetcode704. 二分查找(查找某个元素,简单,不同写法)

指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
随机推荐
BIO模型实现多人聊天
万丈高楼平地起,每个API皆根基
Applied stochastic process 01: basic concepts of stochastic process
RichView TRVStyle 模板样式的设置与使用
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
[daily question] 729 My schedule I
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
Chapter 7 - thread pool of shared model
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Call, apply, bind rewrite, easy to understand with comments
BUU的MISC(不定时更新)
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
PCL realizes frame selection and clipping point cloud
Embed UE4 program into QT interface display
LeetCode 78:子集
首发织梦百度推送插件全自动收录优化seo收录模块
Proteus -- Serial Communication parity flag mode