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 ...
随机推荐
- js 定位到某个锚点
js 定位到某个锚点 html页面内可以设置锚点,锚点定义 <a name="firstAnchor">&nsbp;</a> 锚点使用 <a ...
- 取消mod_sofia的呼叫鉴权
FreeSWITCH中默认的SIP呼叫是要鉴权的,流程如下. 终端 FreeSWITCH A -----Invite------> FS A <----Trying------ FS A ...
- Spark学习(一)--RDD操作
标签(空格分隔): 学习笔记 Spark编程模型的两种抽象:RDD(Resilient Distributed Dataset)和两种共享变量(支持并行计算的广播变量和累加器). RDD RDD是一种 ...
- Walkthrough: Arranging Controls on Windows Forms Using Snaplines
https://msdn.microsoft.com/en-us/library/t5b5kc41(v=vs.110).aspx Spacing and Aligning Controls Using ...
- [51NOD1065] 最小正子段和(STL,前缀和)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1065 估计没人这么做吧-用一个set维护前缀和,但是set的l ...
- 使用httpclient时候,出现“Too many open files”问题
最近接触的sendHttpPostRequest的问题比较多,近期碰到了关于 "java.net.SocketException: Too many open files" 的问题 ...
- 流媒体基础实践之——Nginx-RTMP-module 指令详解
转载网址:http://blog.csdn.net/aoshilang2249/article/details/51483814
- SAP供应商和客户的创建
进来遇到一个创建供应商的需求,由于在系统中关于供应商和客户的创建比较特殊,且没有相关函数进行创建, 找到一个类和方法来创建,类名:VMD_EI_API 方法名:MAINTAIN_DIRECT_INP ...
- http1.1和http1.0区别
http1.1和服务器建立连接可以获得多个资源 http1.0和服务器建立连接可以获取一个值
- Android 呼吸灯流程分析
一.Android呼吸灯Driver实现 1.注册驱动 代码位置:mediatek/kernel/drivers/leds/leds_drv.c 602static struct platform_d ...