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 ...
随机推荐
- 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件
1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...
- Zookeeper 简单操作
1. 连接到zookeeper服务 [java2000_wl@localhost zookeeper-3]$ bin/zkCli.sh -server 127.0.0.1:2181 也可以连接远端的 ...
- hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)
1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...
- java移位运算符实验程序:<<(左移)、>>(带符 号右移)和>>>(无符号右移)
public class txs { static void leftbit(){ int i; //整型长度为32位 ...
- oracle 导出导入不含数据的空库
10g或之前,用exp导出,imp导入,带上rows=n参数 11g或以上,用expdp导出,impdp导入,带上CONTENT = METADATA_ONLY 参数 expdp带上此参数,不导出数据 ...
- SSH三大框架整合配置详细步骤(3)
5 配置Spring2.5 5.1 基础配置 1) 导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...
- C语言进入界面编程准备篇
Win视窗编程和DOS下编程不同,但是类似.Windows应用程序也有它的入口函数,DOS程序中的入口函数是main函数,Windows程序的入口函数是WinMain函数.新建Win32 Applic ...
- instruction set汇总
1 aarch64 它armv8-A架构的一种执行状态,之所以说它是一种执行状态是因为,armv8-A还有aarch32这个执行状态.aarch64是64位执行状态,aarch32是32位的执行状态. ...
- (1)iOS9完美越狱
方式一:同步推越狱,其实用的也是方式二 参考:iOS9.3.5不完美越狱(点击跳转) 方式二:使用impactor越狱. 下载地址:http://www.pc6.com/mac/505285.html
- JavaScript 图片广告自动与手动的切换
1.代码 <html> <head> <script type="text/javascript" src="jquery-1.8.j ...