leetcode Maximal Square

原创 2016年08月31日 14:29:27

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

dp,递归关系式:dp[x][y]=min(dp[x-1][y],dp[x][y-1],dp[x-1][y-1])+1,dp[x][y]代表以x,y为右下顶点的正方形的最长边长,代码:

public int maximalSquare(char[][] matrix) {
      int length=0;
    int[][] res=new int[matrix.length+1][matrix[0].length+1];
    for(int i=0;i<matrix.length;i++){
        for(int j=0;j<matrix[0].length;j++){
            if(matrix[i][j]=='1'){
                res[i+1][j+1]=Math.min(Math.min(res[i][j+1],res[i+1][j]),res[i][j])+1;
                if(res[i+1][j+1]>length) length=res[i+1][j+1];
            }
        }
    }
    return length*length;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[Leetcode]Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...

【动态规划】leetcode - Maximal Square

题目: Maximal Square   Given a 2D binary matrix filled with 0's and 1's, find the largest...

Leetcode 221. Maximal Square

221. Maximal Square Total Accepted: 29932 Total Submissions: 126159 Difficulty: Medium ...

Leetcode Maximal Square

Leetcode Maximal Square 相关代码,本算法使用dp算法完成,主要思想为:一列列的计算,计算每个元素右上解的最大square,并记录以其为右下角的square的信息(左边’1’的长...

leetcode 第221题 Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...

《leetCode》: Maximal Square

《leetCode》: Maximal Square题目 Given a 2D binary matrix filled with 0's and 1's, find the largest s...

[Leetcode 221, Medium] Maximal square

[Leetcode 221, Medium] Maximal square

Maximal Square -- leetcode

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret...

Leetcode: Maximal Square

 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and...

leetcode题解分析_221. Maximal Square(图文分析)

【题目】题目链接 Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)