力扣第221题 最大正方形

力扣第221题 最大正方形

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int size = 0;
        int row = matrix.size();
        if(row == 0) return size;
        int col = matrix[0].size();
        if(col == 0) return size;
        vector<vector<int>> dp(row, vector(col, 0));
        for(int i = 0; i < row; i++)
        {
            for(int j = 0; j < col; j++)
            {
                if(matrix[i][j] != '1')
                    continue;
                if(i == 0 || j == 0)
                    dp[i][j] = 1;
                else
                    dp[i][j] = min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                size = max(size, dp[i][j]);
            }
        }
        return size * size;
    }
};

posted on 2020-05-09 00:17  woodjay  阅读(91)  评论(0编辑  收藏  举报

导航