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 ...
随机推荐
- ubuntu下mysqli_connect()显示未定义,mysqli_fetch_all()显示未定义 解决方法
mysqli_connect()显示未定义解决方法: http://www.cnblogs.com/misoag/archive/2013/01/24/2874439.html 让apache.php ...
- An Example Of Validating Text Item In Oracle Forms Using When-Validate-Item Trigger
Example is given below to validate a Text Item in Oracle Forms with specific rules condition which c ...
- 自定义类型转换器converter
作用:目前将日期转换成string,将string转换成我想要的类型 0509课件里讲 一.数据类型转换在web应用程序中,数据存在两个方向上的转换:1.当提交表单时 表单数据以字符串的形式提交 ...
- Metasploit基础命令
msf > show exploits 列Metasploip的所有可用的渗透测试框架.在MSF终端中可以针对渗透测试中发现的安全漏洞来实施相应的渗透攻击. msf > show auxi ...
- CUBRID学习笔记 8 复制数据库
1 export database 类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...
- linux的计划任务crontab
crontab(全称cron table计划任务列表)是一个用于周期性被执行的任的工具. 相关指令: usage: crontab [-u user] file crontab [ -u user ...
- netty概念
Netty的ChannelFuture在Netty中的所有的I/O操作都是异步执行的,这就意味着任何一个I/O操作会立刻返回,不保证在调用结束的时候操作会执行完成.因此,会返回一个ChannelFut ...
- poj1329Circle Through Three Points(三角形外接圆)
链接 套模板 不知道有没有x,y=0情况,不过这种情况都按+号输出的. #include <iostream> #include<cstdio> #include<cst ...
- Android应用程序构成
一个Android应用程序一般是由以下4个组件构成的: 活动(Activity) 意图(Intent) 服务(Service) 内容提供器(Content Provider) 这4个组件是构成andr ...
- Java Date与SimpleDateFormat
最近在弄一些涉及到时间处理的项目.本来自己写了一个时间转换函数,虽然能用但是过于麻烦而且不够规范,于是学习了下java自带的时间处理的类. public class Timechg { public ...