当前位置:网站首页>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;
}
};
边栏推荐
- 【Hot100】739. 每日温度
- Cif10 actual combat (resnet18)
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
- ROS学习_基础
- 【Hot100】739. 每日溫度
- Raspberry pie 3B update VIM
- 19.段页结合的实际内存管理
- Call, apply, bind rewrite, easy to understand with comments
- UniPro甘特图“初体验”:关注细节背后的多场景探索
- Introduction to ros2 installation and basic knowledge
猜你喜欢

Kubernetes cluster builds ZABBIX monitoring platform

When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward

Development of entity developer database application

SEO学习的最好方式:搜索引擎

ROS学习_基础

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

Depth residual network

首发织梦百度推送插件全自动收录优化seo收录模块

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

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
随机推荐
UniPro甘特图“初体验”:关注细节背后的多场景探索
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
TS基础篇
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Day 246/300 SSH connection prompt "remote host identification has changed!"
开源的网易云音乐API项目都是怎么实现的?
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)
Cif10 actual combat (resnet18)
Wechat official account infinite callback authorization system source code, launched in the whole network
[brush questions] how can we correctly meet the interview?
ROS learning_ Basics
接口自动化测试框架:Pytest+Allure+Excel
前缀和数组系列
librosa音频处理教程
SAP SD发货流程中托盘的管理
18. Multi level page table and fast table
ROS2安装及基础知识介绍
PCL实现选框裁剪点云
3. Business and load balancing of high architecture
idea控制台彩色日志