简体   繁体   English

螺旋矩阵挑战:“ListNode”类型的 null 指针内的成员访问

[英]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 and n , which represent the dimensions of a matrix.给定两个整数mn ,它们表示矩阵的维数。

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:一些问题:

  • The inner loops do not check whether head is nullptr and risk making an invalid reference with head->next内部循环不检查head是否为nullptr并冒着使用head->next进行无效引用的风险
  • As you have defined 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 .由于您已将bottomright定义为包容性(使用m-1n-1初始化它们),因此您需要将内部循环条件相应地与i<=bottomi<=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.

相关问题 运行时错误:“ListNode”类型的 null 指针内的成员访问 - runtime error: member access within null pointer of type 'ListNode' &#39;struct ListNode&#39; 类型的空指针内的成员访问 - member access within null pointer of type 'struct ListNode' 链接列表中的运行时错误:“ListNode”类型的 null 指针中的成员访问 - Runtime error in Linked List :member access within null pointer of type 'ListNode' 运行时错误:null 类型“ListNode”指针内的成员访问 - 克隆链表 - runtime error: member access within null pointer of type 'ListNode' - Clone a linked list 为什么我在“ListNode”类型的 null 指针中获得此错误成员访问权限 - Why am I getting this error member access within null pointer of type 'ListNode' 为什么我会收到此运行时错误:null 类型“Solution::node”(solution.cpp) 指针中的成员访问 - Why am I getting this runtime error: member access within null pointer of type 'Solution::node' (solution.cpp) 调用对象类型“ ListNode *”不是函数或函数指针 - Call object type 'ListNode *' is not a function or function pointer 为什么我在 null 指针错误(链表)中获得成员访问权限 - why do I get member access within null pointer error (linked list) 是否对 C++ 中定义的 null 指针进行成员访问? - Is member access on a null pointer defined in C++? 使用struct作为私有成员在链接列表类中定义ListNode - Using struct as private member to define ListNode within link list class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM