[英]Spiral matrix challenge: member access within null pointer of type 'ListNode'
I am working on LeetCode problem 2326. Spiral Matrix IV :我正在解决 LeetCode 问题2326。螺旋矩阵 IV :
You are given two integers
m
andn
, which represent the dimensions of a matrix.给定两个整数
m
和n
,它们表示矩阵的维数。You are also given the
head
of a linked list of integers.您还将获得一个整数链表的
head
。Generate an
mxn
matrix that contains the integers in the linked list presented in spiral order (clockwise) , starting from the top-left of the matrix.从矩阵的左上角开始,生成一个
mxn
矩阵,其中包含以螺旋顺序(顺时针)呈现的链表中的整数。 If there are remaining empty spaces, fill them with -1.如果还有剩余的空格,则用 -1 填充它们。
Return the generated matrix.
返回生成的矩阵。
I am trying to solve it using 4 different pointers pointing to the edges of the matrix between which our linked list is travelling and storing the value of the linked list node inside the matrix.我正在尝试使用 4 个不同的指针来解决它,这些指针指向我们的链表在其间移动的矩阵的边缘,并将链表节点的值存储在矩阵内。
But I get this error:但我得到这个错误:
Line 46: Char 56: runtime error: member access with null pointer of type 'ListNode' (solution.cpp)
第 46 行:字符 56:运行时错误:使用“ListNode”类型的 null 指针进行成员访问(solution.cpp)
SUMMARY: UndefinedBahaviorSanitizer: indefined-behavior prog_joined.cpp:55:46
摘要:UndefinedBahaviorSanitizer:indefined-behavior prog_joined.cpp:55:46
Here is my code:这是我的代码:
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>>ans(m,vector<int>(n,-1));
int top=0;
int bottom=m-1;
int left=0;
int right=n-1;
int dir=0;
while(head){
switch(dir){
case 0:{
for(int i=left;i<right;i++){
ans[top][i]=head->val;
head=head->next;
}
top++;
dir=(dir+1)%4;
break;
}
case 1:{
for(int i=top;i<bottom;i++){
ans[i][right]=head->val;
head=head->next;
}
right--;
dir=(dir+1)%4;
break;
}
case 2:{
for(int i=right;i>=left;--i){
ans[bottom][i]=head->val;
head=head->next;
}
bottom--;
dir=(dir+1)%4;
break;
}
case 3:{
for(int i=bottom;i>=top;--i){
ans[i][left]=head->val;
head=head->next;
}
left++;
dir=(dir+1)%4;
break;
}
}
}
return ans;
}
};
What is causing the error?是什么导致了错误?
Some issues:一些问题:
head
is nullptr
and risk making an invalid reference with head->next
head
是否为nullptr
并冒着使用head->next
进行无效引用的风险bottom
and right
as inclusive (initialising them with m-1
and n-1
), you would need to align the inner loop condition accordingly with i<=bottom
and i<=right
.bottom
和right
定义为包容性(使用m-1
和n-1
初始化它们),因此您需要将内部循环条件相应地与i<=bottom
和i<=right
对齐。 I would further suggest to not have inner loops, but maintain the current position (row/col) in the matrix and only fill in one value per iteration of the outer loop.我会进一步建议不要有内部循环,但在矩阵中保持当前的 position(行/列),并且每次外部循环的迭代只填写一个值。 This way there is only one test needed for
head
.这样,
head
只需要一个测试。
You can also avoid code repetition and use variables to determine which boundary to check and which change to make to the current position (row/col).您还可以避免代码重复并使用变量来确定要检查哪个边界以及对当前 position(行/列)进行哪些更改。
Here is how that could look:这是看起来的样子:
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> ans(m, vector<int>(n, -1));
int boundary[4] = {0, n-1, m-1, 0}; // Top, Rightend, Bottom, Leftend
int direction[4] = {-1, 1, 1, -1}; // Up, Right, Down, Left
int index[2] = {0, 0}; // Row, Column
int dir = 1; // Right
while (head) {
ans[index[0]][index[1]] = head->val;
head = head->next;
if (index[dir % 2] == boundary[dir]) {
dir = (dir + 1) % 4;
boundary[dir ^ 2] += direction[dir];
}
index[dir % 2] += direction[dir];
}
return ans;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.