Maximal Square 我们都在寻找最高1子矩阵(leeCode)
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)的更多相关文章
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 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 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [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 ...
- [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 ...
随机推荐
- nginx 区分pc和mobile 到不同的404页面
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry|vivo)') { set $mobile_request '1'; } ...
- 一个发送邮件的C++库–jwsmtp
接收邮件的,暂时没搞定,不过在SF上找到了这个发送邮件的库文件.它提供了一个比较完整的类,可以简单的调用发送邮件.下面是作者提供的一个例子,不过由于连SMTP发邮件需要密码,所以代码我改了一下.// ...
- HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)
传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...
- iphone开发中数据持久化之——模型对象归档(二)
在Cocoa世界中,术语“归档”是指另一种形式的序列化,它可以实现对任何对象的序列化.使用对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们.只要在类中实现的每个属性都是标量(如 ...
- java泛型 之 入门(interface)
一:泛型简单介绍: (1)所谓泛型,就是变量类型的參数化. 泛型是JDK1.5中一个最重要的特征.通过引入泛型,我们将获得编译时类型的安全和执行时更小的抛出ClassCastException的可能. ...
- ActionBar本部分适用述评
http://note.youdao.com/share/?id=7f213cb64069bad221f4581507707294&type=note 因为把图片拿进来太麻烦,所以我给了一个直 ...
- 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo
日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo <%@ page contentType="t ...
- HdU 4046 Panda 段树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 意甲冠军:到了bw组成的长度为n的字符串(n<=50000).有m次操作(m<=1000 ...
- Linux下玩转Dota2
Dota2是一款颇为风靡的即时战略类游戏,去年官方就支持Mac和Linux了,对于习惯Mac和linux平台的孩子们来说,简直感动的泪流满面. 当然,也简直是linux程序猿的福音啊啊! 与Win8. ...
- html+css实现登录界面
<!DOCTYPE html> <style type="text/css"> body{ background-color: #555555; } #ti ...