当前位置:网站首页>leetcode-6111:螺旋矩阵 IV
leetcode-6111:螺旋矩阵 IV
2022-07-05 05:46:00 【菊头蝙蝠】
题目
题目连接
给你两个整数:m
和 n
,表示矩阵的维数。
另给你一个整数链表的头节点 head
。
请你生成一个大小为 m x n
的螺旋矩阵,矩阵包含链表中的所有整数。链表中的整数从矩阵 左上角 开始、顺时针 按 螺旋 顺序填充。如果还存在剩余的空格,则用 -1
填充。
返回生成的矩阵。
示例 1:
输入:m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
输出:[[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
解释:上图展示了链表中的整数在矩阵中是如何排布的。
注意,矩阵中剩下的空格用 -1 填充。
示例 2:
输入:m = 1, n = 4, head = [0,1,2]
输出:[[0,1,2,-1]]
解释:上图展示了链表中的整数在矩阵中是如何从左到右排布的。
注意,矩阵中剩下的空格用 -1 填充。
解题
方法一:模拟
螺旋矩阵系列题目都差不多,无非是用链表来取值。
/** * 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:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> matrix(m,vector<int>(n,-1));
vector<vector<int>> dirs={
{
0,1},{
1,0},{
0,-1},{
-1,0}};
int x=0,y=0;
int cur_d=0;
int top=0,down=m-1,left=0,right=n-1;
ListNode* cur=head;
while(cur){
matrix[x][y]=cur->val;
if(cur_d==0&&y==right){
top++;
cur_d++;
}
else if(cur_d==1&&x==down){
right--;
cur_d++;
}
else if(cur_d==2&&y==left){
down--;
cur_d++;
}
else if(cur_d==3&&x==top){
left++;
cur_d++;
}
cur_d%=4;
x+=dirs[cur_d][0];
y+=dirs[cur_d][1];
cur=cur->next;
}
return matrix;
}
};
边栏推荐
- F - Two Exam(AtCoder Beginner Contest 238)
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Mysql database (I)
- High precision subtraction
- Over fitting and regularization
- SAP method of modifying system table data
- [jailhouse article] jailhouse hypervisor
- Sword finger offer 53 - I. find the number I in the sorted array
- 【Jailhouse 文章】Jailhouse Hypervisor
- Gbase database helps the development of digital finance in the Bay Area
猜你喜欢
Little known skills of Task Manager
Scope of inline symbol
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Wazuh开源主机安全解决方案的简介与使用体验
How to adjust bugs in general projects ----- take you through the whole process by hand
wordpress切换页面,域名变回了IP地址
[practical skills] how to do a good job in technical training?
剑指 Offer 58 - II. 左旋转字符串
从Dijkstra的图灵奖演讲论科技创业者特点
剑指 Offer 35.复杂链表的复制
随机推荐
过拟合与正则化
Haut OJ 1401: praise energy
Dynamic planning solution ideas and summary (30000 words)
sync. Interpretation of mutex source code
PC register
Implement an iterative stack
注解与反射
【Jailhouse 文章】Look Mum, no VM Exits
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
游戏商城毕业设计
EOJ 2021.10 E. XOR tree
剑指 Offer 35.复杂链表的复制
剑指 Offer 06.从头到尾打印链表
Daily question - longest substring without repeated characters
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
wordpress切换页面,域名变回了IP地址
How to adjust bugs in general projects ----- take you through the whole process by hand
剑指 Offer 53 - I. 在排序数组中查找数字 I
使用Electron开发桌面应用