c++ solution


  • 0
    Q
    class Solution {
    public:
        int maximalSquare(vector<vector<char>>& matrix) {
            if(matrix.empty() || matrix[0].empty())
                return 0;
            int res = 0, l = matrix.size(), d = matrix[0].size();
            for(int i=0;i<l;i++){
                for(int j=0;j<d;j++){
                    if(matrix[i][j] == '1'){
                        int m = min(l-i,d-j) - 1;
                        int n;
                        for(n=1;n<=m;n++){
                            if(judge(matrix,i,j,n) == false)
                                break;
                        }
                        int t = n*n;
                        res = max(res,t);
                    }
                }
            }
            return res;
        }
        
        bool judge(vector<vector<char>>& matrix, int i, int j, int n){
            for(int k=i;k<=n+i;k++){
                for(int l=j;l<=n+j;l++){
                    if(matrix[k][l] == '0')
                        return false;
                }
            }
            return true;
        }
    };
    

Log in to reply
 

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