221 Maximal Square 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积。
例如,给出如下矩阵:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 4.
详见:https://leetcode.com/problems/maximal-square/description/
Java实现:
建立一个二维dp数组,其中dp[i][j]表示到达(i, j)位置所能组成的最大正方形的边长。首先考虑边界情况,也就是当i或j为0的情况,那么在首行或者首列中,必定有一个方向长度为1,那么就无法组成长度超过1的正方形,最多能组成长度为1的正方形,条件是当前位置为1。一般情况的递推公式:对于任意一点dp[i][j],由于该点是正方形的右下角,所以该点的右边,下边,右下边都不用考虑,关心的是左边,上边,和左上边。只有当前(i, j)位置为1,dp[i][j]才有可能大于0,否则dp[i][j]一定为0。当(i, j)位置为1,此时要看dp[i-1][j-1], dp[i][j-1],和dp[i-1][j]这三个位置,找其中最小的值,并加上1,就是dp[i][j]的当前值了,最后用dp[i][j]的值来更新结果res的值即可。
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix==null||matrix.length==0){
return 0;
} int res=0;
int[][] dp = new int[matrix.length][matrix[0].length]; for(int i=0; i<matrix.length; i++){
dp[i][0]=matrix[i][0]-'0';
res=Math.max(res, dp[i][0]);
} for(int j=0; j<matrix[0].length; j++){
dp[0][j]=matrix[0][j]-'0';
res=Math.max(res, dp[0][j]);
} for(int i=1; i<matrix.length; i++){
for(int j=1; j<matrix[0].length; j++){
if(matrix[i][j]=='1'){
int min = Math.min(dp[i-1][j], dp[i][j-1]);
min = Math.min(min, dp[i-1][j-1]);
dp[i][j]=min+1; res = Math.max(res, min+1);
}else{
dp[i][j]=0;
}
}
} return res*res;
}
}
C++实现:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty())
{
return 0;
}
int row = matrix.size(), col = matrix[0].size();
int res = 0;
vector<vector<int>> dp(row, vector<int>(col, 0));
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (i == 0 || j == 0)
{
dp[i][j] = matrix[i][j] - '0';
}
else if (matrix[i][j] == '1')
{
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j])) + 1;
}
res = max(res, dp[i][j]);
}
}
return res * res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4550604.html
221 Maximal Square 最大正方形的更多相关文章
- [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 ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [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】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 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 re ...
- [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 ...
- (medium)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 ...
随机推荐
- hdu 3549 Flow Problem(最大流模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...
- python的线程thread笔记
python的线程是用thread和threading来实现的.其中利用threading会更好,因为thread没有线程保护,当主线程退出了之后,子线程也会被强行退出.threading支持守护线程 ...
- IDEA失效的解决办法
1.根据下图进行操作即可解决
- redis缓存数据库的详解
1,什么是redis? Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以 ...
- /dev/zero和/dev/null的区别
http://www.cnblogs.com/jacktu/archive/2010/06/28/1766791.html /dev/zero和/dev/null的区别 使用/dev/null 把 ...
- 动态追踪技术 Dynamic Tracing
https://openresty.org/posts/dynamic-tracing/ 工欲性能调优,必先利其器(2)- 火焰图| PingCAP https://pingcap.com/blog- ...
- js数组清空和去重
1.splice var ary = [1,2,3,4]; ary.splice(0,ary.length); console.log(ary); // 输出 Array[0],空数组,即被清空了 2 ...
- 编译Android VNC Server【转】
本文转载自:http://www.cnblogs.com/fengfeng/p/3289292.html 1,在如下地址checkout源代码,我checkout的版本为0.9.7http://cod ...
- YTU 2960: 改错题--小鼠标,你要干什吗?
2960: 改错题--小鼠标,你要干什吗? 时间限制: 1 Sec 内存限制: 128 MB 提交: 118 解决: 62 题目描述 鼠标双击不同的图标产生不同的效果,比如双击文档(documen ...
- CodeForces-213E:Two Permutations(神奇的线段树+hash)
Rubik is very keen on number permutations. A permutation a with length n is a sequence, consisting o ...