在一个由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. 碰撞检測之Sphere-Box检測

    检測思路 首先要做的是将Box转为AABB,然后推断圆心是否在Box内.用的就是之前的SAT 假设圆心在Box内,肯定相交, 假设不在圆心内.则有四种情况,与顶点相交,与楞相交,与面相交,这里的确定也 ...

  2. 【Mongodb教程 第十三课 】PHP mongodb 的增删改查使用

    <pre> <?php #phpinfo();die; #其他链接方式 #$conn=new Mongo(); #连接本地主机,默认端口. #$conn=new Mongo(&quo ...

  3. soapUI系列之—-01 介绍soapUI简介,groovy 简介

    1.soapui简介 SoapUI是一个自由和开放源码的跨平台功能测试解决方案.通过一个易于使用的图形界面和企业级功能,SoapUI让您轻松,快速创建和执行自动化功能.回归.合规和负载测试.在一个测试 ...

  4. web爬虫之登录google paly 商店

    我们先打开Google play 首页 ,点击右上角"登陆"button,即跳到登陆页面 每次我要用爬虫的方式来登陆某个站点的时候,我都会先随便输入一个账号password点击登陆 ...

  5. linux系统编程:线程同步-相互排斥量(mutex)

    线程同步-相互排斥量(mutex) 线程同步 多个线程同一时候訪问共享数据时可能会冲突,于是须要实现线程同步. 一个线程冲突的演示样例 #include <stdio.h> #includ ...

  6. Android之弹出多级菜单

    使用布局文件创建菜单:(多级菜单) 在res下创建目录menu(假设已经有啦就不用再创建了) 在该menu目录下创建XML文件这里我把文件名称命名为menu 在创建的menu.XML文件里 写入: & ...

  7. NYOJ1026 阶乘末尾非0 【模板】

    阶乘末尾非0 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 我们的问题非常是简单.n! 末尾非0数是几? 比方n=5的时候,n! =120,那么n!末尾非0数是2. ...

  8. Android大牛的博客

    1 谦虚的天下:http://www.cnblogs.com/qianxudetianxia/ 2 csdn博文精选:http://www.csdn.net/article/2011-08-30/30 ...

  9. 2016/3/30 租房子 ①建立租房子的增、删、改php页面 ②多条件查询 ③全选时 各部分全选中 任意checkbox不选中 全选checkbox不选中

    字符串的另一种写法:<<<AAAA; 后两个AA回车要求顶格  不然报错 例子: <!DOCTYPE html> <html lang="en" ...

  10. scrapy框架的解析

    1,scrapy框架的官网:https://scrapy.org/ 什么是scrapy框架: scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用内框架,非常出名,所谓框架就是一个已经继承 ...