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(最大正方形)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 方法一 ...

  6. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  7. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  8. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  9. 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 ...

随机推荐

  1. TMS320C6455BCTZA 原厂订购 原装正品

    作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...

  2. docker初学

    Docker基础知识 1. Docker基础知识点 1.1 什么是Docker Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.(集装箱原理) 1.2 什 ...

  3. globalAlpha 示例

    代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. bzoj4455 & loj2091 [Zjoi2016]小星星 容斥原理+树形DP(+状压DP?)

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4455 https://loj.ac/problem/2091 题解 很不错的一道题.(不过在当 ...

  5. 特朗普或出席!富士康耗资100亿美元建LCD液晶面板厂

    富士康建液晶工厂,富士康科技集团发言人证实,富士康科技集团将于今年6月28日耗资100亿美元的LCD面板厂举行动工仪式. 富士康周四表示,他已经了解到,仪式将于今年6月28日举行,包括美国总统特朗普总 ...

  6. Webpack 4 和单页应用入门

    引言 本文转自https://github.com/wallstreetcn/webpack-and-spa-guide,为了方便阅读转到博客园. webpack 更新到了 4.0,官网还没有更新文档 ...

  7. Oracle12c RAC数据导出至Oracle11g

    一.Oracle12c导出数据 1.连接数据库 sqlplus / as sysdba 2.查看pdbs show pdbs; 3.切换pdb alter session set container= ...

  8. 禁止打开 F12 开发者工具

    禁止F12 window.onkeydown = window.onkeyup = window.onkeypress = function (event) { // 判断是否按下F12,F12键码为 ...

  9. kylin相关问题

    1.Kylin踩坑笔记 - 启动Kylin出现Could not find or load main class org.apache.hadoop.hbase.util.GetJavaPropert ...

  10. SGU 194 Reactor Cooling (无源上下界网络流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...