(medium)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 return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
思路借鉴:dynamic programing. 以当前点(x,y) = '1' 为右下角的最大正方形的边长f(x,y) = min( f(x-1,y), f(x,y-1), f(x-1,y-1)) + 1.
代码如下:
public class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix==null || matrix.length==0 || matrix[0].length==0)
return 0;
int n=matrix.length;
int m=matrix[0].length;
int [][]d=new int[n][m];
int max=0;
for(int i=0;i<n;i++){
if(matrix[i][0]=='1'){
d[i][0]=1;
max=1;
}
}
for(int j=0;j<m;j++){
if(matrix[0][j]=='1'){
d[0][j]=1;
max=1;
}
}
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(matrix[i][j]=='0') d[i][j]=0;
else{
d[i][j]=Math.min(Math.min(d[i-1][j],d[i][j-1]),d[i-1][j-1])+1;
max=Math.max(max,d[i][j]);
}
}
}
return max*max; }
}
运行结果:
(medium)LeetCode 221.Maximal Square的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- [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 ...
- Java for 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 ...
- Leetcode 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
随机推荐
- python sys.argv[]
sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...
- android数据存储之Sqlite(一)
SQLite学习笔记 1. Sqlite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低 ...
- Oracle常用知识小结
前言: 前一段时间项目开发数据库环境为Oracle,作为一个SQLer,表示各种不适应.所以刚开始的时候走了一些弯路,浪费了一席时间.因此就想把这些常用的东西给总结一下,算是对自己学习的总结,也希望能 ...
- [原]Fedora 20的yum配置
新装了一套Fedora 20操作系统,又要开始配置yum了.下面总结以下步骤: 1.下载国内比较快的yum源 推荐163的yum源,sohu的yum源也不错,我一般就装第一个,安装163 yum源主页 ...
- 在C#中保存Bouncy Castle生成的密钥对
在用Bouncy Castle的C#版API产生公钥和私钥 中产生了一对密钥对,可以用bouncy caslte提供的API进行保存 公钥方面的3个类,具体代码根据命名空间自行查看其源代码: Org. ...
- 【python】浅谈encode和decode
对于encode和decode,笔者也是根据自己的理解,有不对的地方还请多多指点. 编码的理解: 1.编码:utf-8,utf-16,gbk,gb2312,gb18030等,编码为了便于理解,可以把它 ...
- 请使用-Xlint:deprecation重新编译
[已解决]Android Studio编译OsmAnd出现警告:GeoPointParserUtil.java使用或覆盖了已过时的 API.有关详细信息请使用-Xlint:deprecation重新编 ...
- CGI技术原理
一.CGI技术 1.1 CGI的提出 CGI是外部扩展应用程序与WWW服务器交互的一个标准接口.按照CGI标准编写的外部扩展应用程序可以处理客户端(一般是WWW浏览器)输入的协同工作数据,完成客户端与 ...
- SqlServer统计最近一周的数据
select * from 表名 where DATEDIFF( day, 日期字段列名,getdate())<7 and DATEPART(w, 日期字段列名) <DATEPART( ...
- [tty与uart]stty命令使用
中文解释链接:http://linux.51yip.com/search/stty 英文解释链接:http://pubs.opengroup.org/onlinepubs/9699919799/uti ...