当前位置:网站首页>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,讓地球科學研究更省力
- 19. Actual memory management of segment page combination
- 【Hot100】739. Daily temperature
- Depth residual network
- Misc of BUU (update from time to time)
- Proteus -- Serial Communication parity flag mode
- Fedora/rehl installation semanage
- SQL Server Manager studio (SSMS) installation tutorial
- Classification des verbes reconstruits grammaticalement - - English Rabbit Learning notes (2)
- Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
猜你喜欢

Simple use of MySQL database: add, delete, modify and query

RichView TRVStyle 模板样式的设置与使用
![[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)](/img/3c/c25e7cbef9be1860842e8981f72352.png)
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)

18.多级页表与快表

Fast target recognition based on pytorch and fast RCNN
![[brush questions] how can we correctly meet the interview?](/img/89/a5b874ba4db97fbb3d330af59c387a.png)
[brush questions] how can we correctly meet the interview?

Visitor tweets about how you can layout the metauniverse

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

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

When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
随机推荐
Day 248/300 thoughts on how graduates find jobs
Day 248/300 关于毕业生如何找工作的思考
成功解决TypeError: data type ‘category‘ not understood
P5706 [deep foundation 2. Example 8] redistributing fat house water -- February 13, 2022
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
基于购买行为数据对超市顾客进行市场细分(RFM模型)
Redis Foundation
19. Actual memory management of segment page combination
UDP攻击是什么意思?UDP攻击防范措施
Successfully solved typeerror: data type 'category' not understood
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
How to find a medical software testing institution? First flight software evaluation is an expert
Proteus -- Serial Communication parity flag mode
Simple use of MySQL database: add, delete, modify and query
Blue Bridge Cup zero Foundation National Championship - day 20
A brief introduction of reverseme in misc in the world of attack and defense
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
[brush questions] how can we correctly meet the interview?
顶测分享:想转行,这些问题一定要考虑清楚!