当前位置:网站首页>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;
}
};
边栏推荐
- Pallet management in SAP SD delivery process
- 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
- Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
- PCL实现选框裁剪点云
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- Redis Foundation
- SAP SD发货流程中托盘的管理
- Machine learning plant leaf recognition
- kubernetes集群搭建Zabbix监控平台
- Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
猜你喜欢

顶测分享:想转行,这些问题一定要考虑清楚!

Cif10 actual combat (resnet18)

Lesson 7 tensorflow realizes convolutional neural network

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债

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

A method to measure the similarity of time series: from Euclidean distance to DTW and its variants

Proteus -- Serial Communication parity flag mode

【刷题】怎么样才能正确的迎接面试?

Blue Bridge Cup zero Foundation National Championship - day 20
随机推荐
Day 248/300 thoughts on how graduates find jobs
【每日一题】729. 我的日程安排表 I
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Monotonic stack
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Chapter 7 - thread pool of shared model
Lesson 7 tensorflow realizes convolutional neural network
RichView TRVStyle 模板样式的设置与使用
librosa音频处理教程
万丈高楼平地起,每个API皆根基
C language_ Double create, pre insert, post insert, traverse, delete
L'Ia dans les nuages rend la recherche géoscientifique plus facile
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
医疗软件检测机构怎么找,一航软件测评是专家
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Blue Bridge Cup zero Foundation National Championship - day 20
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting