关闭

leetCode No.221 Maximal Square

标签: leetcode
247人阅读 评论(0) 收藏 举报
分类:

题目

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.

标签:Dynamic Programming
相似问题: (H) Maximal Rectangle

题意

给定一个由01组成的二位矩阵,寻找其中由1组成的最大的正方形并返回其面积。

解题思路

初始化矩阵后,遍历当前矩阵的点,若当前点为1,则以当前点为右下角点。由此可得递推公式:f(x,y) = min(f(x-1)(y),f(x)(y-1),f(x-1)(y-1)) + 1。

代码

public class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix.length == 0) {
            return 0;
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] ret = new int[row][col];
        int max = 0;
        // 初始化列
        for (int i = 0; i < row; i++) {
            if (matrix[i][0] == '1') {
                ret[i][0] = 1;
                max = 1;
            }
        }
        // 初始化行
        for (int i = 0; i < col; i++) {
            if (matrix[0][i] == '1') {
                ret[0][i] = 1;
                max = 1;
            }
        }
        //递推公式:f(x,y) = min(f(x-1)(y),f(x)(y-1),f(x-1)(y-1)) + 1
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (matrix[i][j] == '0') {
                    ret[i][j] = 0;
                }else {
                    ret[i][j] = Math.min(ret[i - 1][j], Math.min(ret[i][j - 1], ret[i - 1][j - 1])) + 1;
                    max = Math.max(max, ret[i][j]);
                }
            }
        }
        return max * max;
    }
}

相关链接

源代码(github)
原题

0
0
查看评论

leetcode 221: Maximal Square

leetcode 221: Maximal Square c++ python java
  • xudli
  • xudli
  • 2015-06-05 08:15
  • 10900

LeetCode221:Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the ...
  • u012501459
  • u012501459
  • 2015-06-18 22:53
  • 1628

LeetCode No.221 Maximal Square

LeetCode No.221 Maximal Square
  • woshihuangjianwei
  • woshihuangjianwei
  • 2016-11-14 22:01
  • 105

[leetcode] 221 Maximal Square(最大全1正方形 &动态规划)

当我们判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况...
  • NK_test
  • NK_test
  • 2015-10-04 23:04
  • 4427

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 return its area.For example, given the following m...
  • guanzhongshan
  • guanzhongshan
  • 2015-08-10 21:53
  • 483

221. Maximal Square | Java最短代码实现

原题链接:221. Maximal Square 【思路】 本题考查动态规划。我们知道当 maxtrix[i][j] = '1' 时,以 matrix[i][j] 为正方形右下角的边长,最多比以 matrix[i - 1][j]、matrix[i][j - 1]、matri...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-24 10:20
  • 561

436.Maximal Square-最大正方形(中等题)

最大正方形 题目在一个二维01矩阵中找到全为1的最大正方形 样例1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 题解定义相同大小的辅助数组res,且第一例和第一行元素与原数组相同。从res[1][1]开始,如果原数组matrix[i][j]为0则表...
  • Tri_Color_Flag
  • Tri_Color_Flag
  • 2016-11-19 13:25
  • 175

leetcode_question_85 Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题目就是给一个矩阵,找一个全是一的最大子矩阵...
  • doc_sgl
  • doc_sgl
  • 2013-09-19 17:29
  • 14386

Maximal Square 最大的正方形

Maximal Square   Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area....
  • u012605629
  • u012605629
  • 2015-06-12 20:11
  • 358

LeetCode OJ 之 Maximal Square (最大的正方形)

题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, give...
  • u012243115
  • u012243115
  • 2015-08-22 10:36
  • 694
    个人资料
    • 访问:15149次
    • 积分:482
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论