IndexofBound exception in Java


  • 0
    B

    Runtime Error Message:Line 21: java.lang.ArrayIndexOutOfBoundsException: 1
    Last executed input: ["1"]

    public int maximalSquare(char[][] matrix) {
            if (matrix == null || matrix.length == 0|| matrix[0].length == 0) return 0;
            int length = matrix.length;
            int layer = 0;
            int row = 0;
            int column = 0;
            while(row + layer < length){
                while (column + layer < length){
                    layer = Math.max(layer, checkLayer(row,column,layer,matrix));
                    if (layer == 0) column++;
                    column += layer;
                }
                if (layer == 0) row++;
                row += layer;
            }
            return layer*layer;
        }
        
        public int checkLayer(int row, int column, int layer, char[][] matrix){
            if (matrix[row][column] == '0') return 0;
            for(int i = 1; i < layer; i++){
                if (matrix[row-i][column] == '0' || matrix[row][column-i] =='0') return 0;
            }
            return 1+ checkLayer(row+1,column+1,layer+1,matrix);
        }
    

    I think my code have already handle 0,0 situation. but I still get this error. plz help.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.