簡體   English   中英

螺旋矩陣挑戰:“ListNode”類型的 null 指針內的成員訪問

[英]Spiral matrix challenge: member access within null pointer of type 'ListNode'

我正在解決 LeetCode 問題2326。螺旋矩陣 IV

給定兩個整數mn ,它們表示矩陣的維數。

您還將獲得一個整數鏈表的head

從矩陣的左上角開始,生成一個mxn矩陣,其中包含以螺旋順序(順時針)呈現的鏈表中的整數。 如果還有剩余的空格,則用 -1 填充它們。

返回生成的矩陣。

我正在嘗試使用 4 個不同的指針來解決它,這些指針指向我們的鏈表在其間移動的矩陣的邊緣,並將鏈表節點的值存儲在矩陣內。

但我得到這個錯誤:

第 46 行:字符 56:運行時錯誤:使用“ListNode”類型的 null 指針進行成員訪問(solution.cpp)

摘要:UndefinedBahaviorSanitizer:indefined-behavior prog_joined.cpp:55:46

這是我的代碼:

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;
    }
};

是什么導致了錯誤?

一些問題:

  • 內部循環不檢查head是否為nullptr並冒着使用head->next進行無效引用的風險
  • 由於您已將bottomright定義為包容性(使用m-1n-1初始化它們),因此您需要將內部循環條件相應地與i<=bottomi<=right對齊。

我會進一步建議不要有內部循環,但在矩陣中保持當前的 position(行/列),並且每次外部循環的迭代只填寫一個值。 這樣, head只需要一個測試。

您還可以避免代碼重復並使用變量來確定要檢查哪個邊界以及對當前 position(行/列)進行哪些更改。

這是看起來的樣子:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM