【题目】

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

【思路】

  1. dp square面积和三个有关
  2. 注意特殊空集条件

【代码】

class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length==0)
return 0;
int dp[][]=new int[matrix.length+1][matrix[0].length+1];
int ans=0; for(int i=1;i<matrix.length+1;i++){
for(int j=1;j<matrix[0].length+1;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;
ans=
Math.max(ans,dp[i][j]);
}
}
}
return ans*ans;
}
}

[Leetcode221]最大面积 Maximal Square的更多相关文章

  1. [Swift]LeetCode221. 最大正方形 | Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  2. 动态规划-最大的正方形面积 Maximal Square

    2018-09-13 19:19:44 问题描述: 问题求解: 方法一: 使用动态规划来求解,算法时间复杂度O(n^2). dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长. 初始 ...

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

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

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

  5. 【刷题-LeetCode】221. Maximal Square

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

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

  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】221. Maximal Square

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

随机推荐

  1. cookie知识点

    1.springmvc框架中,cookie例子 jsp: <%-- Created by IntelliJ IDEA. User: 44262 Date: 2019/2/28 Time: 18: ...

  2. Servlet Exception and Error Handling

    Servlet API support for custom Exception and Error Handler servlets that we can congiure in deployme ...

  3. 【转】 聚类算法-Kmeans算法的简单实现

    1. 聚类与分类的区别: 首先要来了解的一个概念就是聚类,简单地说就是把相似的东西分到一组,同 Classification (分类)不同,对于一个 classifier ,通常需要你告诉它“这个东西 ...

  4. 上传RNA-seq数据到NCBI GEO数据库

    SRA - NCBI example - NCBI 要发文章了,审稿时编辑肯定会要求你上传NGS测序数据. 一般数据都是放在集群,不可能放在个人电脑上,因为有的数据大的吓人(几个T). 所以我们就建一 ...

  5. English Voice of <<Bye Bye Bye>>

    Bye Bye Bye - Lovestoned When i see you, looking back at me 当我看到你回首看我时 Watching this eye still 彼此凝视 ...

  6. php正则的使用

    函数 描述 preg_filter 执行一个正则表达式搜索和替换 preg_grep 返回匹配模式的数组条目 preg_last_error 返回最后一个PCRE正则执行产生的错误代码 preg_ma ...

  7. loj#2020. 「AHOI / HNOI2017」礼物

    题意:给定xy数组求 \(\sum_{i=0}^{n-1}(x_i+y_{(i+k)\modn}+c)^2\) 题解:先化简可得 \(n*c^2+2*\sum_{i=0}^{n-1}x_i-y_i+\ ...

  8. JdbcTemplate查询返回JavaBean的几种方法

    关于JdbcTemplate的官方描述如下: org.springframework.jdbc.core.JdbcTemplate 大约的讲,将JdbcTemplate返回的list结果集生成Java ...

  9. [NOIP 2014TG D1T3] 飞扬的小鸟

    题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便 ...

  10. linux -- 基于zookeeper搭建yarn的HA高可用集群

    linux -- 基于zookeeper搭建yarn的HA高可用集群 实现方式:配置yarn-site.xml配置文件 <configuration> <property> & ...