(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 ...
随机推荐
- 现在流行什么 JS库/框架?
现在大家最感兴趣的 JS 库和框架是什么? jQuery 91.5% Underscore 38.6% AngularJS 28.5% Backbone 18.6% React 15.7% Knock ...
- C转义字符
C语言输出特殊字符 C语言转义字符意义大体同于前面的C#转义字符,这里列出用c语言,输出%d.\n等特殊字符的方法. #include <stdio.h> int main() { pri ...
- DataRow[] 转为数组
DataRow[] rows = dt.Select("1=1"); ].ToString()).ToArray();
- scala之method和function的区别
在我没有学习scala的时候,主要用java和python做日常工作开发,印象中,没有特别的刻意的去区分method和function的区别,这个关系,正如我们日常生活中,没有刻意的去区分质量和重量. ...
- [Hibernate] - Annotations
Hibernate使用Annotations最简单例子: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8 ...
- JS 黑客帝国文字下落效果
黑客帝国文字下落效果 源代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- HackerRank "Playing with numbers"
This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...
- 解密 Uber 数据团队的基础数据架构优化之路
如果你用过Uber,你一定会注意到它的操作是如此的简单.你一键叫车,随后车就来找你了,最后自动完成支付,整个过程行云流水.但是,在这简单的流程背后其实是用Hadoop和Spark这样复杂的基础大数据架 ...
- item3 二维数组中的查找[剑指offer]
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有这个整数? 8 9 思路:查找7 ...
- 黄聪:Discuz!的SEO优化策略二:如何去掉页脚多余的信息
论坛搭建好,首先是把多余的东西都砍掉. 页脚的信息在我看来,都是很多余的信息,如下图: 要怎么消灭掉它们呢? 1.进入 全局 -- 站点信息 2.站点名称改为你的论坛名称,它会出现在内页的标题最末位. ...