当前位置:网站首页>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;
}
};
边栏推荐
- AI on the cloud makes earth science research easier
- SSO流程分析
- Office doc add in - Online CS
- Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
- Simple use of MySQL database: add, delete, modify and query
- 云上有AI,让地球科学研究更省力
- 3. Business and load balancing of high architecture
- pymongo获取一列数据
- Visitor tweets about how you can layout the metauniverse
- L'Ia dans les nuages rend la recherche géoscientifique plus facile
猜你喜欢

Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent

How to find a medical software testing institution? First flight software evaluation is an expert

机器学习植物叶片识别

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

26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...

C语言_双创建、前插,尾插,遍历,删除

ROS学习_基础

On the first day of clock in, click to open a surprise, and the switch statement is explained in detail

What is the biggest problem that fresh e-commerce is difficult to do now

Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
随机推荐
PCL realizes frame selection and clipping point cloud
攻防世界 MISC中reverseMe简述
Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
编译,连接 -- 笔记 -2
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
Delete external table source data
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
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
Apache dolphin scheduler source code analysis (super detailed)
[brush questions] how can we correctly meet the interview?
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
[Yu Yue education] flower cultivation reference materials of Weifang Vocational College
Huawei equipment configuration ospf-bgp linkage
Refer to how customer push e-commerce does content operation
Simple use of JWT
19. Actual memory management of segment page combination
Apache DolphinScheduler源码分析(超详细)
Call, apply, bind rewrite, easy to understand with comments
Simple use of MySQL database: add, delete, modify and query