当前位置:网站首页>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;
}
};
边栏推荐
- [jailhouse article] look mum, no VM exits
- CPU内核和逻辑处理器的区别
- 【Rust 笔记】15-字符串与文本(下)
- 网络工程师考核的一些常见的问题:WLAN、BGP、交换机
- 个人开发的渗透测试工具Satania v1.2更新
- 【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Real time clock (RTC)
- Common optimization methods
- 【Rust 笔记】15-字符串与文本(上)
猜你喜欢

redis发布订阅命令行实现

开源存储这么香,为何我们还要坚持自研?

Individual game 12

RGB LED infinite mirror controlled by Arduino

LVS简介【暂未完成(半成品)】
![R language [import and export of dataset]](/img/5e/a15ab692a6f049f846024c98820fbb.png)
R language [import and export of dataset]

Educational Codeforces Round 116 (Rated for Div. 2) E. Arena

Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module

Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135

Wazuh開源主機安全解决方案的簡介與使用體驗
随机推荐
Control unit
Convolution neural network -- convolution layer
2017 USP Try-outs C. Coprimes
F - Two Exam(AtCoder Beginner Contest 238)
leetcode-6109:知道秘密的人数
LeetCode 0107.二叉树的层序遍历II - 另一种方法
1996. number of weak characters in the game
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
【Jailhouse 文章】Jailhouse Hypervisor
[practical skills] technical management of managers with non-technical background
【云原生】微服务之Feign自定义配置的记录
打印机脱机时一种容易被忽略的原因
Personal developed penetration testing tool Satania v1.2 update
Sqlmap tutorial (II) practical skills I
RGB LED infinite mirror controlled by Arduino
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
SPI 详解
Overview of variable resistors - structure, operation and different applications
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Common optimization methods