当前位置:网站首页>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;
}
};
边栏推荐
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
- 配置树莓派接入网络
- Pallet management in SAP SD delivery process
- [daily question] 729 My schedule I
- 攻防世界 MISC中reverseMe简述
- Top test sharing: if you want to change careers, you must consider these issues clearly!
- Latex文字加颜色的三种办法
- Cif10 actual combat (resnet18)
- pymongo获取一列数据
- Do you really know the use of idea?
猜你喜欢

leetcode35. 搜索插入位置(简单,找插入位置,不同写法)

呆错图床系统源码图片CDN加速与破解防盗链功能

L'Ia dans les nuages rend la recherche géoscientifique plus facile

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

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

Pallet management in SAP SD delivery process

ROS2安装及基础知识介绍

升级版手机检测微信工具小程序源码-支持多种流量主模式

雲上有AI,讓地球科學研究更省力

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
随机推荐
AI on the cloud makes earth science research easier
Visitor tweets about how you can layout the metauniverse
Do you really know the use of idea?
Thought map of data warehouse construction
What is the biggest problem that fresh e-commerce is difficult to do now
配置树莓派接入网络
UDP攻击是什么意思?UDP攻击防范措施
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
leetcode59. 螺旋矩阵 II(中等)
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
万丈高楼平地起,每个API皆根基
WPF之MVVM
[daily question] 729 My schedule I
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
leetcode6109. 知道秘密的人数(中等,周赛)
Leetcode - 152 product maximum subarray
将ue4程序嵌入qt界面显示
Database basics exercise part 2
TS Basics
Embed UE4 program into QT interface display