当前位置:网站首页>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;
}
};
边栏推荐
- Windows Server 2016 standard installing Oracle
- 机器学习植物叶片识别
- Introduction and underlying analysis of regular expressions
- Do you really know the use of idea?
- 云上有AI,让地球科学研究更省力
- LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
- Fedora/rehl installation semanage
- LeetCode - 152 乘积最大子数组
- Call, apply, bind rewrite, easy to understand with comments
- hydra常用命令
猜你喜欢
How to reconstruct the class explosion caused by m*n strategies?
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
Cif10 actual combat (resnet18)
mysql的基础命令
Development of entity developer database application
My seven years with NLP
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
ROS学习_基础
Leetcode - 152 product maximum subarray
随机推荐
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Windows Server 2016 standard installing Oracle
UDP攻击是什么意思?UDP攻击防范措施
SSO process analysis
Leetcode - 152 product maximum subarray
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
kubernetes集群搭建Zabbix监控平台
云上有AI,让地球科学研究更省力
WPF之MVVM
How to reconstruct the class explosion caused by m*n strategies?
Redis Foundation
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
Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
【Hot100】739. 每日温度
Leetcode daily question (1870. minimum speed to arrive on time)
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
基于购买行为数据对超市顾客进行市场细分(RFM模型)
[hot100] 739. Température quotidienne