[Leetcode221]最大面积 Maximal Square
【题目】
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 square面积和三个有关
- 注意特殊空集条件
【代码】
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的更多相关文章
- [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 ...
- 动态规划-最大的正方形面积 Maximal Square
2018-09-13 19:19:44 问题描述: 问题求解: 方法一: 使用动态规划来求解,算法时间复杂度O(n^2). dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长. 初始 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 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 ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [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每日解题思路 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】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
随机推荐
- 【转】 H.264编码原理以及I帧B帧P帧
转自:http://www.cnblogs.com/herenzhiming/articles/5106178.html 前言 ----------------------- H264是新一代的编码标 ...
- BGP - 4,BGP的三张表
1,BGP的三张表 邻居表(adjancy table) BGP表(forwarding database):BGP默认不做负载均衡,会选出一条最优的,放入路由表 路由表 ...
- Alibaba Java诊断工具Arthas之快速安装和简单使用
Alibaba Java诊断工具Arthas简单介绍 : 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 1.这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception ...
- JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。
英文原文 https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them 摘译(文章内的代码有些过期 ...
- 【洛谷p2430】严酷的训练
(这个题有一个很神奇的地方) 严酷的训练[传送门] 算法标签(显然01背包了最近一直在练) (他居然没写……) 这个题啊,试了好几遍没a 最后发现在第二层循环的时候应该是j>=rqyt[p[i] ...
- Python基础之文件的基本操作
概述:文件的基本操作1.open 打开文件 f = open("xxx",mode="r",encoding="utf-8") #常用形式 ...
- nginx是什么,如何使用
一:nginx是什么? 二:nginx作为网关,需要具备什么?(nginx可以作为web服务器,但更多的时候,我们把它作为网关,因为它具备网关必备的功能:) 反向代理(反向代理就是服务器找来一个机器代 ...
- Linux tar压缩命令 排除某个目录 (根据man tar 总结)
一般直接用tar命令打包很简单,直接使用 tar -zcvf test.tar.gz test 即可. 在很多时候,我们要对某一个目录打包,而这个目录下有几十个子目录和子文件,我们需要在打包的时候排除 ...
- 如何定位jdk中的native方法源码?
前提条件:已下载openjdk的源码. 以System类的arraycopy为例: 1. 根据关键字定位文件:grep -rn '"arraycopy"' ./openjdk关键字 ...
- 使用AndroidStudio运行eclipse开发的app项目
由于AS和eclipse开发的APP项目格式不同,所以直接用AS运行eclipse项目是行不通的. 下面给大家讲解一下如何在AS上成功运行eclipse项目 首先有这么个eclipse项目文件夹 然后 ...