【题目】

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. mysqlsh : mysql shell tutorial

    MySQL Shell 是一个高级的命令行客户端以及代码编辑器for Mysql. 除了SQL,MySQL Shell也提供脚本能力 for JS and Python. When MySQL she ...

  2. causal snps | causal variants | tensorflow | 神经网络实战 | Data Simulation

    先读几篇文章: Interpretation of Association Signals and Identification of Causal Variants from Genome-wide ...

  3. python 中的 and / or

    逻辑运算符:and   or    not 优先级:       not > and > or 数字:0为假, 非0为真: 字符串:空为假,非空为真: 逻辑表达式的值: x   and   ...

  4. English trip V1 - 20.Look at me 看着我 Teacher:Solo Key: 声调(英语默认就声调[rising]和降调[falling]两种)

    In this lesson you will learn to describe a person. 课上内容(Lesson) appearance  -> ap pea ran ce  外貌 ...

  5. 20170920xlVBA_FTP_UpDownLoad_DownLoad

    '建立应用环境进程 Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpen ...

  6. You Don't Know JS: Scope & Closures (第4章: Hoisting)

    Chapter4: Hoisting 变量附加到哪个层次的scope,由它们在哪里和如何声明(let, var)来决定. Function scope/Block scope都有相同的法则:任何变量在 ...

  7. Confluence 6 应该如何在我的空间中组织内容

    页面和博客 你在 Confluence 中创建的任何内容,从会议记录到回顾和任何中间的内容,不管来源是博客和页面. 你的主页将是任何访问你网站中的用户首先看到的内容.为了让用户更加容易的找到他们需要查 ...

  8. drf 生成接口文档

    REST framework可以自动帮助我们生成接口文档.接口文档以网页的方式呈现. 自动接口文档能生成的是继承自APIView及其子类的视图. 一.安装依赖 REST framewrok生成接口文档 ...

  9. drf 需求案例1

    案例: 实现过程: 1. 创建一个项目: django-adim startproject dfr3 2.  创建 一个app    homwork python manage.py startapp ...

  10. bzoj2431

    题意:求有多少个逆序对为k的排列 题解:\(dp[i][j]\)表示1~i的排列中有j个逆序对的方案数,转移就是把i放在1~i-1的排列中的第几位,\(dp[i][j]=\sum_{x=0}^{min ...