其他分享
首页 > 其他分享> > 221. 最大正方形 Maximal Square

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

 

标签:Maximal,tmp,Square,area,max,min,length,221,define
来源: https://blog.csdn.net/ZRXSLYG/article/details/111964155