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. [Android学习笔记]jackson库的使用

    Jackson库一般用于序列化和反序列化操作,通常会涉及到的操作是: 1. Java Object -> Json String 2. Java Object -> Xml String ...

  2. Load and Unload

    一.前言 在前一段时间,我遭遇了一个现象诡异的Bug,最后原因归结为在DllMain里错误地调用了FreeLibrary(在本文最后对此Bug有详细的解释). MSDN里关于禁止在DllMain里调用 ...

  3. git使用ssh密钥和https两种认证方式汇总(转)

    在版本库的SSH方式和HTTPS方式是不同的,具体来说就是url信息的不同,但是,实际的认证机制也是不同的.当建立了本机密钥之后,使用ssh方式实际上是不需要再次认证的,而https则每次需要输入密码 ...

  4. vim php代码规范

    vim 代码规范工具php-cs-fixer.phar (參考https://github.com/FriendsOfPHP/PHP-CS-Fixer) INSTALL curl http://get ...

  5. IE8,IE9,IE10,FireFox 的CSS HACK

    #employeesView { top: 732px; //所有浏览器 top: 730px\9;//所有IE浏览器 } @media all and (min-width:0) { #employ ...

  6. 网络安全审查制度即将推出 手机App安全加密成必定趋势

    年05月22日宣布,为维护国家网络安全.保障中国用户合法利益,中国即将推出网络安全审查制度,关系国家安全和公共利益的系统使用的.重要信息技术产品和服务,应通过网络安全审查.文章出处:*** 网络安全审 ...

  7. JarSearch

    个人做的小工具分享给大家~~. 支持从压缩文件搜索文件,特别是根据部分类文件名在jar里查找文件,比较方便,效率也还不错. 也支持从目录查找 http://pan.baidu.com/s/1feYaM ...

  8. hdu4614(线段树+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给定一个区间[0,N-1],初始时每个位置上的数字都是0,可以对其进行以下两种操作: 1. ...

  9. mahout源码KMeansDriver分析之五CIMapper

    接上文重点分析map操作: Vector probabilities = classifier.classify(value.get());// 第一行 Vector selections = pol ...

  10. NYOJ 104 最大子矩阵(二维DP)

    最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 给定一个由整数组成二维矩阵(r*c),如今须要找出它的一个子矩阵,使得这个子矩阵内的全部元素之和最大,并把这个 ...