在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积。
例如,给出如下矩阵:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 4.

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

Java实现:

建立一个二维dp数组,其中dp[i][j]表示到达(i, j)位置所能组成的最大正方形的边长。首先考虑边界情况,也就是当i或j为0的情况,那么在首行或者首列中,必定有一个方向长度为1,那么就无法组成长度超过1的正方形,最多能组成长度为1的正方形,条件是当前位置为1。一般情况的递推公式:对于任意一点dp[i][j],由于该点是正方形的右下角,所以该点的右边,下边,右下边都不用考虑,关心的是左边,上边,和左上边。只有当前(i, j)位置为1,dp[i][j]才有可能大于0,否则dp[i][j]一定为0。当(i, j)位置为1,此时要看dp[i-1][j-1], dp[i][j-1],和dp[i-1][j]这三个位置,找其中最小的值,并加上1,就是dp[i][j]的当前值了,最后用dp[i][j]的值来更新结果res的值即可。

class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix==null||matrix.length==0){
return 0;
} int res=0;
int[][] dp = new int[matrix.length][matrix[0].length]; for(int i=0; i<matrix.length; i++){
dp[i][0]=matrix[i][0]-'0';
res=Math.max(res, dp[i][0]);
} for(int j=0; j<matrix[0].length; j++){
dp[0][j]=matrix[0][j]-'0';
res=Math.max(res, dp[0][j]);
} for(int i=1; i<matrix.length; i++){
for(int j=1; j<matrix[0].length; j++){
if(matrix[i][j]=='1'){
int min = Math.min(dp[i-1][j], dp[i][j-1]);
min = Math.min(min, dp[i-1][j-1]);
dp[i][j]=min+1; res = Math.max(res, min+1);
}else{
dp[i][j]=0;
}
}
} return res*res;
}
}

C++实现:

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty())
{
return 0;
}
int row = matrix.size(), col = matrix[0].size();
int res = 0;
vector<vector<int>> dp(row, vector<int>(col, 0));
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (i == 0 || j == 0)
{
dp[i][j] = matrix[i][j] - '0';
}
else if (matrix[i][j] == '1')
{
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
}
res = max(res, dp[i][j]);
}
}
return res * res;
}
};

参考:https://www.cnblogs.com/grandyang/p/4550604.html

221 Maximal 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 ...

  2. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  3. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  4. 【刷题-LeetCode】221. Maximal Square

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

  5. [LintCode] Maximal Square 最大正方形

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

  6. 【LeetCode】221. Maximal Square

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

  7. 221. Maximal Square -- 矩阵中1组成的最大正方形

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

  8. [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 ...

  9. (medium)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 ...

随机推荐

  1. Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)

    Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...

  2. 笔记本 ThinkPad E40 安装 Mac OS X 10.9.3 Mavericks 系统

    关于:自己最早接触Mac OS X系统是在一个论坛里.记得好像是2011年:那时论坛里就有人在虚拟机上执行Mac OS X 10.7系统.当时也依照论坛里的方法在虚拟机上成功装上了系统.那时開始就被苹 ...

  3. 1.5.4 HAVING子句

    1.5.4 HAVING子句正在更新内容.请稍后

  4. graph driver-device mapper-03thin pool基本操作

    // 在thin pool中创建一个新thin device // 调用路径:driver.Create() 1.1 func (devices *DeviceSet) AddDevice(hash, ...

  5. 3.2.1 配置构建Angular应用——简单的笔记存储应用——展示功能

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  6. Java - split()函数和trim()函数的使用方法

    split()函数和trim()函数的使用方法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24465141 详细參考Java ...

  7. 2016/2/26 <marquee></marquee>实现多种滚动效果

    页面的自动滚动效果,可由javascript来实现,但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标记 ...

  8. servlet,RMI,webservice之间的区别

    最近项目中有提供或者调用别的接口,在纠结中到底是用servlet还是用webservice,所以上网查看了下他们以及RMI之间的区别,方便加深了解. 首先比较下servlet和webservice下  ...

  9. QmlWinExtras

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zhengtianzuo06/article/details/78404961QtWinExtras是 ...

  10. Cg入门23: Fragment shader – UV动画(序列帧)

    让动画从1-9循环播放此纹理 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...