当前位置:网站首页>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;
}
};
边栏推荐
- CF1634 F. Fibonacci Additions
- 【Rust 笔记】13-迭代器(下)
- [practical skills] how to do a good job in technical training?
- [article de jailhouse] jailhouse hypervisor
- leetcode-22:括号生成
- Individual game 12
- CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
- The connection and solution between the shortest Hamilton path and the traveling salesman problem
- Simple knapsack, queue and stack with deque
- How many checks does kubedm series-01-preflight have
猜你喜欢
CF1637E Best Pair
leetcode-6110:网格图中递增路径的数目
[cloud native] record of feign custom configuration of microservices
Real time clock (RTC)
【Jailhouse 文章】Jailhouse Hypervisor
Brief introduction to tcp/ip protocol stack
QQ电脑版取消转义符输入表情
[jailhouse article] jailhouse hypervisor
CF1634E Fair Share
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
随机推荐
打印机脱机时一种容易被忽略的原因
MIT-6874-Deep Learning in the Life Sciences Week 7
Some common problems in the assessment of network engineers: WLAN, BGP, switch
SQLMAP使用教程(一)
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
实时时钟 (RTC)
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
Personal developed penetration testing tool Satania v1.2 update
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
开源存储这么香,为何我们还要坚持自研?
Overview of variable resistors - structure, operation and different applications
Full Permutation Code (recursive writing)
Dichotomy, discretization, etc
CF1634E Fair Share
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Daily question 2006 Number of pairs whose absolute value of difference is k
Implement an iterative stack
CF1634 F. Fibonacci Additions
Configuration and startup of kubedm series-02-kubelet
leetcode-9:回文数