当前位置:网站首页>leetcode59. 螺旋矩阵 II(中等)
leetcode59. 螺旋矩阵 II(中等)
2022-07-06 06:55:00 【重you小垃】
思路:模拟
具体思路:如果遇到边缘或者遇到之前访问过的改变方向
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;
}
};
边栏推荐
- 成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
- SAP SD发货流程中托盘的管理
- Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
- Short video, more and more boring?
- 开源的网易云音乐API项目都是怎么实现的?
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- PCL realizes frame selection and clipping point cloud
- The difference between get and post request types
- The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
- Proteus -- Serial Communication parity flag mode
猜你喜欢
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Suspended else
kubernetes集群搭建Zabbix监控平台
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
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Huawei equipment configuration ospf-bgp linkage
Development of entity developer database application
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Apache DolphinScheduler源码分析(超详细)
雲上有AI,讓地球科學研究更省力
随机推荐
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
医疗软件检测机构怎么找,一航软件测评是专家
接口自动化测试框架:Pytest+Allure+Excel
Number of query fields
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Proteus -- Serial Communication parity flag mode
How to find a medical software testing institution? First flight software evaluation is an expert
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)
18.多级页表与快表
18. Multi level page table and fast table
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Database basics exercise part 2
Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
Redis Foundation
Lesson 7 tensorflow realizes convolutional neural network
mysql的基础命令
Chapter 7 - thread pool of shared model