当前位置:网站首页>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;
}
};
边栏推荐
- 漏了监控:Zabbix对Eureka instance状态监控
- Blue Bridge Cup zero Foundation National Championship - day 20
- TS Basics
- What is the biggest problem that fresh e-commerce is difficult to do now
- 微信公众号无限回调授权系统源码 全网首发
- Pallet management in SAP SD delivery process
- librosa音频处理教程
- 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
- Introduction to ros2 installation and basic knowledge
- idea控制台彩色日志
猜你喜欢

配置树莓派接入网络

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

“无聊猿” BAYC 的内忧与外患

The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module

RichView TRVStyle 模板样式的设置与使用

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

WPF之MVVM

What is the biggest problem that fresh e-commerce is difficult to do now

ROS2安装及基础知识介绍

Hydra common commands
随机推荐
Embed UE4 program into QT interface display
Depth residual network
Establishment and operation of cloud platform open source project environment
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
leetcode841. 钥匙和房间(中等)
RichView TRVStyle 模板样式的设置与使用
[daily question] 729 My schedule I
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
pymongo获取一列数据
Huawei equipment configuration ospf-bgp linkage
Cif10 actual combat (resnet18)
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
首发织梦百度推送插件全自动收录优化seo收录模块
ROS learning_ Basics
leetcode59. 螺旋矩阵 II(中等)
Entity Developer数据库应用程序的开发
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
[brush questions] how can we correctly meet the interview?
A brief introduction of reverseme in misc in the world of attack and defense
BIO模型实现多人聊天