当前位置:网站首页>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;
}
};
边栏推荐
- [English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
- 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
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- 【刷题】怎么样才能正确的迎接面试?
- Delete external table source data
- Chapter 7 - thread pool of shared model
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- ROS学习_基础
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- Day 248/300 thoughts on how graduates find jobs
猜你喜欢
What is the difference between int (1) and int (10)? Senior developers can't tell!
mysql的基础命令
SAP SD发货流程中托盘的管理
3. Business and load balancing of high architecture
Fedora/REHL 安装 semanage
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
开源的网易云音乐API项目都是怎么实现的?
Introduction and underlying analysis of regular expressions
机器学习植物叶片识别
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
随机推荐
Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
L'Ia dans les nuages rend la recherche géoscientifique plus facile
BIO模型实现多人聊天
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
Leetcode - 152 product maximum subarray
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
UWA Pipeline 2.2.1 版本更新说明
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
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)
医疗软件检测机构怎么找,一航软件测评是专家
LeetCode - 152 乘积最大子数组
Proteus -- Serial Communication parity flag mode
基于PyTorch和Fast RCNN快速实现目标识别
librosa音频处理教程