当前位置:网站首页>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;
}
};
边栏推荐
- Web APIs DOM node
- F - Two Exam(AtCoder Beginner Contest 238)
- 2022年贵州省职业院校技能大赛中职组网络安全赛项规程
- Pointnet++ learning
- Daily question 2013 Detect square
- Alu logic operation unit
- Sword finger offer 53 - I. find the number I in the sorted array
- How many checks does kubedm series-01-preflight have
- 【云原生】微服务之Feign自定义配置的记录
- CF1634 F. Fibonacci Additions
猜你喜欢
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Brief introduction to tcp/ip protocol stack
Sword finger offer 09 Implementing queues with two stacks
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Web APIs DOM node
sync. Interpretation of mutex source code
剑指 Offer 04. 二维数组中的查找
[jailhouse article] look mum, no VM exits
A misunderstanding about the console window
SAP method of modifying system table data
随机推荐
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
CF1634 F. Fibonacci Additions
Smart construction site "hydropower energy consumption online monitoring system"
Binary search template
个人开发的渗透测试工具Satania v1.2更新
Csp-j-2020-excellent split multiple solutions
Kubedm series-00-overview
挂起等待锁 vs 自旋锁(两者的使用场合)
剑指 Offer 05. 替换空格
剑指 Offer 04. 二维数组中的查找
The sum of the unique elements of the daily question
Pointnet++ learning
Daily question 2006 Number of pairs whose absolute value of difference is k
Haut OJ 2021 freshmen week II reflection summary
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
884. Uncommon words in two sentences
剑指 Offer 53 - II. 0~n-1中缺失的数字
Introduction et expérience de wazuh open source host Security Solution
Codeforces round 712 (Div. 2) d. 3-coloring (construction)