美文网首页
221. Maximal Square

221. Maximal Square

作者: becauseyou_90cd | 来源:发表于2018-07-26 23:45 被阅读0次

    https://leetcode.com/problems/maximal-square/description/

    class Solution {
    public int maximalSquare(char[][] matrix) {

        int row = matrix.length;
        if(row == 0) return 0;
        int col = matrix[0].length;
        int[][] dp = new int[row][col];
        int sum = 0;
        for(int i = 0; i < row; i++){
            if(matrix[i][0] == '1'){
                dp[i][0] = 1;
                sum = Math.max(sum, dp[i][0]);
            }else{
                dp[i][0] = 0;
            }
        }
        for(int i = 0; i < col; i++){
            if(matrix[0][i] == '1'){
                dp[0][i] = 1;
                sum = Math.max(sum, dp[0][i]);
            }else{
                dp[0][i] = 0;
            }
        }
        for(int i = 1; i < row; i++){
            for(int j = 1; j < col; j++){
                if(matrix[i][j] == '0')
                    dp[i][j] = 0;
                else{
                    dp[i][j] = Math.min(Math.min(dp[i-1][j-1],dp[i-1][j]), dp[i][j-1])+1;
                    sum = Math.max(sum, dp[i][j]);
                }
            }
        }
        return (int)Math.pow(sum, 2);
    }
    

    }

    相关文章

      网友评论

          本文标题:221. Maximal Square

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