当前位置:网站首页>Leetcode-6111: spiral matrix IV
Leetcode-6111: spiral matrix IV
2022-07-05 06:09:00 【Chrysanthemum headed bat】
leetcode-6111: Spiral matrix IV
subject
Topic linking
Here are two integers :m
and n
, Represents the dimension of the matrix .
Another head node of the integer linked list head
.
Please generate a size of m x n
The spiral matrix of , The matrix contains all integers in the linked list . The integers in the linked list are from the matrix top left corner Start 、 Clockwise Press screw Fill in order . If there are still remaining spaces , Then use -1
fill .
Return the generated matrix .
Example 1:
Input :m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
Output :[[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
explain : The figure above shows how the integers in the linked list are arranged in the matrix .
Be careful , The remaining spaces in the matrix are used -1 fill .
Example 2:
Input :m = 1, n = 4, head = [0,1,2]
Output :[[0,1,2,-1]]
explain : The figure above shows how the integers in the linked list are arranged from left to right in the matrix .
Be careful , The remaining spaces in the matrix are used -1 fill .
Problem solving
Method 1 : simulation
Spiral matrix series topics are similar , It's nothing more than using a linked list to get values .
/** * 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;
}
};
边栏推荐
- [rust notes] 16 input and output (Part 1)
- Configuration and startup of kubedm series-02-kubelet
- SQLMAP使用教程(二)实战技巧一
- Daily question 1342 Number of operations to change the number to 0
- CF1637E Best Pair
- leetcode-31:下一个排列
- [practical skills] technical management of managers with non-technical background
- Appium foundation - use the first demo of appium
- 剑指 Offer II 058:日程表
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
猜你喜欢
Appium automation test foundation - Summary of appium test environment construction
1.13 - RISC/CISC
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
个人开发的渗透测试工具Satania v1.2更新
数据可视化图表总结(一)
QQ电脑版取消转义符输入表情
EOJ 2021.10 E. XOR tree
【Jailhouse 文章】Jailhouse Hypervisor
MIT-6874-Deep Learning in the Life Sciences Week 7
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
随机推荐
Introduction et expérience de wazuh open source host Security Solution
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Convolution neural network -- convolution layer
[rust notes] 17 concurrent (Part 2)
Simple knapsack, queue and stack with deque
[practical skills] technical management of managers with non-technical background
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
leetcode-6111:螺旋矩阵 IV
leetcode-31:下一个排列
Transform optimization problems into decision-making problems
Daily question 2013 Detect square
Flutter Web 硬件键盘监听
CF1634E Fair Share
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
[article de jailhouse] jailhouse hypervisor
QQ电脑版取消转义符输入表情
【实战技能】如何做好技术培训?
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
PC register