221. Maximal Square

dp[i][j]代表当前最大边长: 左边,上面,左斜对角线的最小值加1,res中存储当前最大边长

public static int maximalSquare(char[][] matrix) {
    if (matrix == null || matrix.length == 0) return 0;
    int res = 0;
    int row = matrix.length;
    int col = matrix[0].length;
    int[][] dp = new int[row + 1][col + 1];
    for (int i = 1; i <= row; i++) {
        for (int j = 1; j <= col; j++) {
            if (matrix[i - 1][j - 1] == '1') {
                dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                res = Math.max(res, dp[i][j]);
            }
        }
    }
    return res * res;
}

results matching ""

    No results matching ""