【题目】

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. 学了vue和webpack的笔记

    首先把package.json贴出来,这里很多插件存在版本区别,因此要特别注意版本,不是所有的安装最新的都行 { "name": "life_manager", ...

  2. socket 发送图片

    using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using Syste ...

  3. PHP操作MySQL数据库--PHP的应用

    一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...

  4. pandas更换index,column名称

    1)仅换掉index名称 df.index = list 2)调整index时,后面的项目也要跟着调整: df.reindex(list) 注意如果list中出现了df中没有的index,后面的项目会 ...

  5. SqlSever查询某个表的列名称、说明、备注、注释,类型等

    这周整理了数据库文档,发现用导出脚本来整理表的信息注释查看不方便,因此我就想能不能SQL语句查询表的注释或者表的字段.我就我问朋友是不是可以,他给我点指导,然后自己也在网上百度,来实现自己的想法,我把 ...

  6. bat语法需要注意的地方

    if else 格式                            if exist C:\Python27  ::空格  (                      ::(与if在同一行  ...

  7. poj-2888-矩阵+polya

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 6195   Accepted: 1969 D ...

  8. SpringBoot配置多数据源时遇到的问题

    SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...

  9. 基本数据类型dict

    1. 字典 dict 用{}来表示 键值对数据 {key:value} 唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 可哈希不可变的数据类型:int str tuple bool ...

  10. IIS无法启动,应用程序池自动关闭

    问题:打开网站中的资源,对应的应用程序池就自动停止 解决方案:在应用程序池上--右键--高级设置--进程模型--标识,更改了这项里的“内置账户”.将原有的“ApplicationPoolIdentit ...