My AC Java Solution with O(1) Space(Made some changes to original matrix)


  • 1
    O
    public int maximalSquare(char[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return 0;
        }
        int result = matrix[0][0] - '0';
        for(int row = 0; row < matrix.length; row++){
            for(int col = 0; col < matrix[0].length; col++){
                if(row == 0 || col == 0){
                    if(matrix[row][col] == '1' && result == 0){
                        result = 1;
                    }
                } else if(matrix[row][col] == '1'){
                    int cur = Math.min(Math.min(matrix[row - 1][col - 1], matrix[row][col - 1]), matrix[row - 1][col]) -'0' + 1;
                    matrix[row][col] = (char)(cur + '0');
                    if(cur > result){
                        result = cur;
                    }
                }
            }
        }
        return result*result;
    }

Log in to reply
 

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