Note:

For square, the minimum size of  topleft and top and left decides the square of the result.

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix.length == 0) return 0;
        int result = 0;
        int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
        for (int i = 1; i <= matrix.length; i++) {
            for (int j = 1; j <= matrix[0].length; 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;
                    result = Math.max(dp[i][j], result);
                }
            }
        }
        return result*result;
    }
}

 

posted on 2017-09-18 13:14  keepshuatishuati  阅读(88)  评论(0编辑  收藏  举报