美文网首页
221. 最大正方形

221. 最大正方形

作者: justonemoretry | 来源:发表于2021-09-03 22:29 被阅读0次
    image.png

    解法

    class Solution {
        public int maximalSquare(char[][] matrix) {
            int row = matrix.length;
            int col = matrix[0].length;
            // 以对应坐标为正方形右下角,能得到的边长最大值
            int[][] dp = new int[row][col];
            int maxSide = 0; 
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (matrix[i][j] == '1') {
                        if (i == 0 || j == 0) {
                            dp[i][j] = 1;
                        } else {
                            // 左上角3个位置最小值加1
                            dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
                        }
                        maxSide = Math.max(dp[i][j], maxSide);
                    }
                }
            }
            return maxSide * maxSide;
        }
    }
    

    相关文章

      网友评论

          本文标题:221. 最大正方形

          本文链接:https://www.haomeiwen.com/subject/nyrtwltx.html