当前位置:网站首页>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;
}
};
边栏推荐
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- Call, apply, bind rewrite, easy to understand with comments
- Entity Developer数据库应用程序的开发
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- LeetCode每日一题(1997. First Day Where You Have Been in All the Rooms)
- 从autojs到冰狐智能辅助的心里历程
- 编译,连接 -- 笔记 -2
- My seven years with NLP
- [advanced software testing step 1] basic knowledge of automated testing
- UWA Pipeline 2.2.1 版本更新说明
猜你喜欢
Fedora/rehl installation semanage
A brief introduction of reverseme in misc in the world of attack and defense
机器学习植物叶片识别
hydra常用命令
攻防世界 MISC中reverseMe简述
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
基于PyTorch和Fast RCNN快速实现目标识别
LeetCode - 152 乘积最大子数组
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Proteus -- Serial Communication parity flag mode
随机推荐
leetcode6109. 知道秘密的人数(中等,周赛)
Day 248/300 thoughts on how graduates find jobs
C语言_双创建、前插,尾插,遍历,删除
BUU的MISC(不定时更新)
My creation anniversary
Chapter 7 - thread pool of shared model
The difference between get and post request types
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
ROS2安装及基础知识介绍
Office doc add in - Online CS
Reflex WMS中阶系列3:显示已发货可换组
Short video, more and more boring?
开源的网易云音乐API项目都是怎么实现的?
Huawei equipment configuration ospf-bgp linkage
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
Call, apply, bind rewrite, easy to understand with comments
Fast target recognition based on pytorch and fast RCNN
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Development of entity developer database application
Basic commands of MySQL