【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 return its area.
For example, given the following matrix:
1 1
1 1
Return 4.
思路:
DP,到达当前(非0)结点的最长边长square[i][j] = min(square[i-1][j-1], min(square[i-1][j], square[i][j-1])) + 1,即取左上方3个结点中值最小的一个加1。代码写的看起来好不舒服 - -!
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
const int rows = matrix.size();
if(rows == )
return ;
vector<vector<char> >::iterator it = matrix.begin();
const int columns = (*it).size();
int square[rows][columns], ret = ;
for(int i = ; i < rows; i++)
for(int j = ; j < columns; j++)
square[i][j] = ;
for(int i = ; i < rows; i++){
if(matrix[i][] == '')
{
square[i][] = ;
ret = ;
}
}
for(int i = ; i < columns; i++){
if(matrix[][i] == '')
{
square[][i] = ;
ret = ;
}
}
for(int i = ; i < rows; i++)
{
for(int j = ; j < columns; j++)
{
square[i][j] = matrix[i][j] == '' ? min(square[i-][j], min(square[i][j-], square[i-][j-])) + : ;
if(square[i][j] > ret)
ret = square[i][j];
}
}
return ret * ret;
}
};
【LeetCode 221】Maximal Square的更多相关文章
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
- 【LeetCode题解】349_两个数组的交集
目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- 【LeetCode题解】144_二叉树的前序遍历
目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...
随机推荐
- FFMpeg那些事——独立运行的二进制文件ffmpeg编译
想编译一个Android可用的二进制文件(要求:支持libx264/liblamemp3) github资源: 1.首先编译一个带lame库的ffmpeg https://github.com/aks ...
- 读写txt文件
public void SetUpdateTime(string strNewDate) { try { var path =Application.StartupPath + Configurati ...
- hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just String http://www.bnuoj.co ...
- stringgird中使用TClientDataSet排序的问题
function TfrmMain.createIIReport(cdsBody: TClientDataSet; silent: Boolean): String;var s,sText: ...
- Matlab中编译C++文件
今天在跑<Robust Object Tracking via Sparsity-based Collaborative Model>这篇文章的代码时候,发现出现如下错误: 发现错误时由于 ...
- *[hackerrank]Sam and sub-strings
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53 ...
- Python list去重及找出,统计重复项
http://bbs.chinaunix.net/thread-1680208-1-1.html 如何找出 python list 中有重复的项 http://www.cnblogs.com/feis ...
- Android:密码显示隐藏
效果: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- MyEclipse 2014GA 新建 Web Project 并配置 SSH
基本软件配置: 1)MyEclipse 2014GA(JDK:内置 1.7.0.u45:SSH:内置 Struts2.1.Spring3.1 和 Hibernate4.1) 2)apache- ...
- Regex Tester 安装教程
下载com.brosinski.eclipse.regex_1.4.0.jar 地址:https://github.com/sbrosinski/RegexTester 下载之后把jar包粘贴到${e ...