50.Maximal Square(最大正方形)
Level
Medium
题目描述:
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
思路分析:
动态规划思想,要找到最大的正方形,我们以dp[ i ] [ j ]表示,以第i行第j列的1元素为正方形的右下角的正方形边长。从左上角开始,如果当前位置为1,那么到当前位置包含的最大正方形边长为左/左上/上的值中的最小值加一,因为边长是由短板控制的。注意返回的是面积,不要因为小问题而出错。
代码:
public class Solution{
public int maximalSquare(char [][]matrix){
if(matrix==null||matrix.length==0)
return 0;
int [][]dp=new int [matrix.length+1][matrix[0].length+1]; //dp[i][j]表示,以第i行第j列的1元素为正方形的右下角的正方形边长
int result=0;
for(int i=1;i<=matrix.length;i++){
for(int j=1;j<=matrix[0].length;j++){
if(matrix[i-1][j-1]=='1'){
dp[i][j]=Math.min(Math.min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
result=Math.max(result,dp[i][j]);
}
}
}
return result*result;
}
}
50.Maximal Square(最大正方形)的更多相关文章
- [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] 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 最大正方形
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 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...
- Leetcode221. 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 方法一 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
随机推荐
- JS高级 — 函数的声明和表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 基于mesos 安装 jenkins
mesos master 机子上安装 jenkins git clone https://github.com/jenkinsci/mesos-plugin.git && cd me ...
- Centos7 安装 clamav
环境 CentOS: 7.x 下载 下载地址 :http://www.clamav.net/downloads,使用目前最新版本为:clamav-0.101.3 使用 wget 下载 wget htt ...
- 330-支持PXIE带FMC接口的Xilinx FPGA XC7K325T PCIeX8 接口卡平台
支持PXIE带FMC接口的Xilinx FPGA XC7K325T PCIeX8 接口卡平台 一.板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_ ...
- 解决图片插入word文档后清晰度降低的问题
解决图片插入word文档后清晰度降低的问题 在默认情况下,word程序会自动压缩插入word文档中的图片以减小整个word文档的.当我们需要插入word文档中的图片保持原始清晰度时,可以通过设置wor ...
- Git--03 git分支
目录 Git分支 1.新建testing分支 2.合并分支 3.合并冲突 4.删除分支 Git标签使用 1.查看标签 02.删除标签 Git分支 分支即是平行空间,假设你在为某个手机系统研发拍照功 ...
- mongodb 多表关联处理 : 内嵌以及连接(手动引用、DBref) 、aggregate中$lookup
MongoDB与关系型数据库的建模还是有许多不同,因为MongoDB支持内嵌对象和数组类型.MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).那么何时Embed何时Li ...
- vscode舒适的字体风格
首选项-->设置-->输入setting.json-->查找到设置文件 添加如下配置 "editor.tabSize": 2, "files.assoc ...
- find按照文件大小查找
例如,find -size +1M:查找大于 1 MB 的文件.其他参数: b: 512-byte blocks. This is the default if no unit is specifie ...
- python字符串非空判断
1. 字符串非空判断 2. list 非空判断