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 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.
class Solution {
public:
inline int min(int x, int y) {
return x<y? x:y;
}
inline int min(int x, int y, int z) {
return min(x, min(y, z));
}
int maximalSquare(vector<vector<char>>& matrix) {
int row = matrix.size();
if (row <=) return ;
int col = matrix[].size();
int maxSize = ;
vector<vector<int>> dp(row, vector<int>(col));
for (int i=; i<matrix.size(); i++) {
for (int j=; j<matrix[i].size(); j++){
//convert the `char` to `int`
dp[i][j] = matrix[i][j] -'';
//for the first row and first column, or matrix[i][j], dp[i][j] is ZERO
//so, it's done during the previous conversion
// i>0 && j>0 && matrix[i][j]=='1'
if (i!= && j!= & dp[i][j]!=){
dp[i][j] = min(dp[i-][j], dp[i][j-], dp[i-][j-]) + ;
}
//tracking the maxSize
if (dp[i][j] > maxSize ){
maxSize = dp[i][j];
}
}
}
return maxSize*maxSize;
}
};
* 1) P[0][j] = matrix[0][j] (topmost row);
* 2) P[i][0] = matrix[i][0] (leftmost column);
* 3) For i > 0 and j > 0:
* 3.1) if matrix[i][j] = 0, P[i][j] = 0;
* 3.2) if matrix[i][j] = 1, P[i][j] = min(P[i-1][j], P[i][j-1], P[i-1][j-1]) + 1.
221. Maximal Square -- 矩阵中1组成的最大正方形的更多相关文章
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- LeetCode题解:(221) Maximal Square
题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- 【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] 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 ...
- 221. Maximal Square
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 221 Maximal Square 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...
随机推荐
- .bash_profile和.bashrc的区别,ubuntu下为.profile,没有.bash_profile
.bash_profile 开机自动加载,比如java的环境变量放在里面 .bashrc打开shell或终端就会加载该文件,比如起的别名或快捷方式放里面.alias设置就在其中. 还有一个.profi ...
- dup2()函数的使用,
#define STR "xiamanman\n"#define STR_LEN 10#define STDOUT 1 #include <stdio.h>#inclu ...
- servlet&jsp高级:第一部分
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- LTE Module User Documentation(翻译4)—— 使用 Fading Trace
LTE用户文档 (如有不当的地方,欢迎指正!) 7 使用 Fading Trace 本节描述如何在 LTE 仿真中使用 fading traces . (1)生成 Fading Traces ...
- UIButton、UIImageView、UILabel的选择
UIButton特点既能显示文字,又能显示图片(能显示2张图片,背景图片.内容图片)长按高亮的时候可以切换图片\文字直接通过addTarget...方法监听点击 UIImageView能显示图片,不能 ...
- JavaWeb 7 Servlet
7 Servlet Servlet学习的大纲:1. servlet概念及相关接口简介2. servet 执行过程3. servlet路径映射4. 缺省servlet --应用5. s ...
- gO语言的安装和环境变量的配置
一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...
- jackson annotations注解详解 (zhuan)
http://blog.csdn.net/sdyy321/article/details/40298081 ************************************** 官方WIKI: ...
- [转]使用git命令上传代码
http://jiajing.elastos.org/2013/04/15/%E4%BD%BF%E7%94%A8git%E5%91%BD%E4%BB%A4%E4%B8%8A%E4%BC%A0%E4%B ...
- semantic-ui使用gulp执行build-css报错
1.执行gulp build-css报错 [09:40:49] Starting 'build-css'... Building CSS Potentially unhandled rejection ...