刷java算法题出现了空指针错误

public class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        Boolean[] rows = new Boolean[m];
        Boolean[] cols = new Boolean[n];
        
        for(int i = 0; i< m; i++){
            for(int j = 0; j< n; j++){
                if(matrix[i][j] == 0){
                    rows[i] = true;
                    cols[j] = true;
                }
            }
        }
        for(int i = 0; i<m; i++){
            for(int j = 0; j< n; j++){
                if(rows[i] ==true || cols[j] == true)
                    matrix[i][j] = 0;   
            }
           
        }
    }
}

该题是leetcode第73题Set Matrix Zeroes,题目如下: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
我的做法是,加一个行数组、一个列数组,来记录哪些行、哪些列有0,但是提交时出现了空指针错误。

长风秋雁
浏览 411回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java