当前位置:网站首页>LeetCode 6111. 螺旋矩阵 IV
LeetCode 6111. 螺旋矩阵 IV
2022-07-05 18:12:00 【HumbleFool】
偏移量法写螺旋矩阵
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
vector<vector<int>> spiralMatrix(int n, int m, ListNode* head) {
vector<vector<int>> res(n, vector<int>(m, -1));
int x = 0, y = 0, d = 1;
for(int i = 0; i < n * m && head; i ++)
{
res[x][y] = head -> val;
int a = a + dx[d], b = b + dy[d];
if(a < 0 || a >= n || b < 0 || b >= m || res[a][b] != -1)
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
head = head -> next;
}
return res;
}
};
边栏推荐
- Crontab 日志:如何记录我的 Cron 脚本的输出
- Star ring technology data security management platform defender heavy release
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- Sibling components carry out value transfer (there is a sequence displayed)
- jdbc读大量数据导致内存溢出
- [TestLink] testlink1.9.18 solutions to common problems
- [use electron to develop desktop on youqilin]
- 《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
- 彻底理解为什么网络 I/O 会被阻塞?
- Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
猜你喜欢
随机推荐
Clickhouse (03) how to install and deploy Clickhouse
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
node_exporter内存使用率不显示
集群部署如何解决海量视频接入与大并发需求?
FCN: Fully Convolutional Networks for Semantic Segmentation
pytorch yolov5 训练自定义数据
Login and connect CDB and PDB
Gimp 2.10 tutorial "suggestions collection"
小白入门NAS—快速搭建私有云教程系列(一)[通俗易懂]
使用JMeter录制脚本并调试
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
Eliminate the writing of 'if () else{}'
【在優麒麟上使用Electron開發桌面應】
Electron安装问题
ISPRS2022/云检测:Cloud detection with boundary nets基于边界网的云检测
[PM2 details]
[paddleclas] common commands
记一次使用Windbg分析内存“泄漏”的案例
Electron installation problems