当前位置:网站首页>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;
}
};
边栏推荐
- Pymongo gets a list of data
- 云上有AI,让地球科学研究更省力
- How to reconstruct the class explosion caused by m*n strategies?
- 成功解决TypeError: data type ‘category‘ not understood
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
- Latex文字加颜色的三种办法
- SSO process analysis
- Successfully solved typeerror: data type 'category' not understood
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- UDP攻击是什么意思?UDP攻击防范措施
猜你喜欢

《从0到1:CTFer成长之路》书籍配套题目(周更)

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

My creation anniversary

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略

SQL Server Manager studio (SSMS) installation tutorial

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

ROS2安装及基础知识介绍

【每日一题】729. 我的日程安排表 I

Development of entity developer database application

Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
随机推荐
SSO process analysis
Day 248/300 关于毕业生如何找工作的思考
18. Multi level page table and fast table
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Pymongo gets a list of data
BIO模型实现多人聊天
Machine learning plant leaf recognition
AI on the cloud makes earth science research easier
一文读懂简单查询代价估算
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Blue Bridge Cup zero Foundation National Championship - day 20
Number of query fields
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
SSO流程分析
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Data security -- 13 -- data security lifecycle management
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
Reflex WMS medium level series 3: display shipped replaceable groups
Development of entity developer database application
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励