221. 最大正方形 Maximal Square

题目 <https://leetcode-cn.com/problems/maximal-square/>

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

int maximalSquare(char** matrix, int matrixSize, int* matrixColSize){
    int **length = malloc(sizeof(int*)*matrixSize);
    int i,j,k;
    for(i=0;i<matrixSize;i++){
        length[i] = malloc(sizeof(int) * matrixColSize[i]);
    }

    int area = 0,area_tmp,n,m;
    for(i=0;i<matrixSize;i++){
        for(j=0;j<matrixColSize[i];j++){
            if(matrix[i][j] == '0'){
                length[i][j] = 0;
                continue;
            }
            if(j-1>=0){
                length[i][j] = length[i][j-1]+1;
            }else{
                length[i][j] = 1;
            }

            for(k=i,n=1,m=length[i][j];k>=0;k--,n++){
                m = min(m,length[k][j]);
                if(n>m){
                    break;
                }
            }
            //printf("%d %d %d\n",i,j,n-1);
            area_tmp = (n-1);
            area_tmp = area_tmp*area_tmp;
            area = max(area,area_tmp);
        }
    }

    for(i=0;i<matrixSize;i++){
        free(length[i]);
    }
    free(length);
    return area;
}

 

上一篇:郭哥优化余弦距离代码记录


下一篇:Tensor和NumPy相互转换