My simple c++ code


  • 0

    class Solution {
    public:
    int maximalSquare(vector<vector<char>>& matrix) {

        int bian = 0;
        vector<vector<int>> mb;
        for(int i=0;i<matrix.size();i++){
            vector<int> mc(matrix[i].size(),0);
            mb.push_back(mc);
        }
        for(int i=0;i<matrix.size();i++){
            for(int j=0;j<matrix[i].size();j++){
                int dim1 = i==0 ? 0 : mb[i-1][j];
                int djm1 = j==0 ? 0 : mb[i][j-1];
                int dim1jm1 = i==0 || j==0 ? 0 : mb[i-1][j-1];
                mb[i][j] = matrix[i][j]=='0' ? 0 : min(dim1,min(djm1,dim1jm1))+1;
                bian = max(bian,mb[i][j]);
            }
        }
        return bian*bian;
    }
    

    };


Log in to reply
 

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