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 matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
  /*
     * 动态规划的算法 
     *if(m[i][j] = 1) d[i][j] = min(d[i-1][j-1], d[i][j-1], d[i-1][j]) + 1
     *m[i][j] = 0; d[i][j] = 0;     
     *初始化 d[0][j] = m[0][j]; d[i][0] = m[i][0];
     *优化思路用一行d[j] 进行
     *int preNode = d[0];
     *d[0] = m[i][0];
     * for(int j = 1; j < n; j++)
     * { 
     *   if(m[i][j] = 1) int temp = min(preNode, d[j-1], d[j]) + 1;
     *   preNode = d[j];
     *   d[j] = temp;
     * }
     *错误1:没有考虑<1,0,1,1>向量 ;把矩阵想成等宽高的
     */
int min(int a, int b)
{
return a < b ? a : b;
}
int min(int a, int b, int c)
{
return min(min(a,b),min(b,c));
}
int maximalSquare(vector<vector<char>>& m) {
if(m.size() == 0) return 0;
int m_size = m.size();
// if(m_size == 1) return (m[0][0] == '0') ? 0 : 1;
int* d = new int[m[0].size()];
int max = 0;
//init
for(int i = 0; i < m[0].size(); i++)
{
d[i] = (m[0][i] == '0') ? 0 : 1;
if(d[i] > max) max = d[i];
}
//循环
for(int l = 1; l < m_size; l++) //从第1行開始
{
int preNode = d[0];
d[0] = (m[l][0] == '0') ? 0 : 1;
for(int j = 1; j < m[0].size(); j++)
{
if(m[l][j] == '0')
{
preNode = d[j];
d[j] = 0;
}
else //m[l][j] = 1时
{
int temp = min(preNode, d[j-1], d[j]);//d[l-1][j-1], d[l][j-1], d[l-1][j]
preNode = d[j];
d[j] = temp + 1;
if(d[j] > max)
{
max = d[j];
}
}
}
}
delete d;
return max*max;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Maximal Square 我们都在寻找最高1子矩阵(leeCode)的更多相关文章

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

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

  2. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

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

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

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

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

  5. 【动态规划】leetcode - Maximal Square

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

  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. 【刷题-LeetCode】221. Maximal Square

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

  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. [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. 小议common lisp程序开发流程 - Ever 17 - 博客频道 - CSDN.NET

    小议common lisp程序开发流程 - Ever 17 - 博客频道 - CSDN.NET 小议common lisp程序开发流程 分类: lisp 2011-04-17 20:59 1316人阅 ...

  2. 动态分析maillog日志,把恶意链接直接用防火墙禁止

    近期用 postfix + dovecot 搭建了一个邮件server, 被人当做垃圾邮件转发器,经过配置postfix 的黑白名单, postfix 提示成功的 REJECT 了垃圾邮件, 只是还是 ...

  3. Selenium: 空指针error

    Error 类型:空指针 可能原因一: 只是引用了该类,但是没有对该类进行实例化(即没有New 一下),即没有给该类分配内存,所以导致空指针: 类调用前注意要实例化,否则会导致空指针错误. 首先声明D ...

  4. Samba &amp; Nginx - Resource temporarily unavailable

    先说说本人的开发环境:Win7 + Editplus + VMware(Centos+Samba+Nginx).用Samba在Centos上把web文件夹(如www)共享,然后在Win7上訪问这个文件 ...

  5. dedecms 文章列表和频道列表同时调用

    演示效果:http://www.mypf110.com/qcd/ <div class="changshi_wrap"> {dede:channelartlist ro ...

  6. jQuery 复制节点的元素实现加入到购物车功能

    描写叙述: 用户点击左边div中的商品,相应商品会自己主动加入到右面的div中,类似电子商城中的加入到购物车功能. 主要用到了jquery中的复制节点功能,基本原理是首先获取点击的元素,然后将对应信息 ...

  7. WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法

    因URL意外地以“/方法名”结束,请求格式无法识别. 执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息. 解决方法:在webservice的we ...

  8. hdu3006(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题意:给定一些集合,求这些集合所能组合的集合个数. 分析:这题就是用一个二进制数保存一个集合的元 ...

  9. WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心

    原文:WPF界面设计技巧(11)-认知流文档 & 小议WPF的野心 流文档是WPF中的一种独特的文档承载格式,它的书写和呈现方式都很像HTML,它也几乎具备了HTML的绝大多数优势,并提供了更 ...

  10. centos 6.5 升级内核 linux 3.12.17

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:Linux centos 2.6.32-431.el6.x86_64(Centos-6.5-x86_64-mi ...