在一个由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. 【Facebook的UI开发框架React入门之八】Image的使用简单介绍(iOS平台)-goodmao

    --------------------------------------------------------------------------------------------------- ...

  2. POJ3264Balanced Lineup(最基础的线段树)

    採用一维数组建树. (由于一维数组建的是全然二叉树,时间上比用孩子节点指针建树慢.只是基本能够忽略=-=) #include<iostream> #include<cstdio> ...

  3. hdu 2059 龟兔赛跑 (dp)

    /* 把起点和终点比作加油站,那总共同拥有n+2个加油站了, 每次都求出从第0个到第j个加油站(j<i)分别在加满油的情况下到第i个加油站的最短时间dp[i], 终于的dp[n+1]就是最优解了 ...

  4. Interpret bytes as packed binary data

    7.1. struct — Interpret bytes as packed binary data — Python 3.6.5 documentation https://docs.python ...

  5. Latex 3: 解决LaTeX编译卡顿问题

    1.问题: 最近在编译latex时,老是在tulmr.fd处编译很久,但是以前不这样啊,那肯定就是我最近做了什么导致这样的了,是什么呢? 2.解决: 后来google下发现了解决办法,原来是我新安装了 ...

  6. Lightoj 1014 - Ifter Party

    I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited  ...

  7. caioj1272&&codeforces 148D: 概率期望值3:抓老鼠

    这道真的是好题,不卡精度,不卡细节,但是思考的方式很巧妙! 一开始大家跟我想的应该差不多,用f[i][j]表示有i只白老鼠,j只黑老鼠的胜率,然后跑DP,然后我就发现,这样怎么做?还有一种不胜不负的平 ...

  8. JSONArray ja = JSONArray.fromObject(list);//特殊类 用于将list转化为JSON 数据并返回 out.print(ja);

    JSONArray ja = JSONArray.fromObject(list);//特殊类 用于将list转化为JSON 数据并返回out.print(ja);

  9. 苹果Instruments/Shark性能调试工具概述

    在Mac OS X上你可以使用Gprof这样的UNIX工具用于测试程序性能.当然,Apple也有自己的Profiling Tools,用得比较多的是Shark.10.5里还引入了一个基于DTrace的 ...

  10. 【转】axios全攻略

    随着 vuejs 作者尤雨溪发布消息,不再继续维护vue-resource,并推荐大家使用 axios 开始,axios 被越来越多的人所了解.本来想在网上找找详细攻略,突然发现,axios 的官方文 ...