本题用brute force超时。可以用DP,也可以不用。

dp[i][j] 代表 以(i,j)为右下角正方形的边长。

 class Solution(object):
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix:
return 0 m = len(matrix)
n = len(matrix[0])
res = 0
dp = [[0 for x in range(n)] for y in range(m)] for i in range(m):
if matrix[i][0] == '':
dp[i][0] = 1
res = 1 for j in range(n):
if matrix[0][j] == '':
dp[0][j] = 1
res = 1 for i in range(1,m):
for j in range(1,n):
if matrix[i][j] == '':
dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j],dp[i][j-1])) + 1
res = max(res, dp[i][j]) return res * res

Leetcode 221. Maximal Square的更多相关文章

  1. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  2. [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 ...

  3. 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 ...

  4. (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 ret ...

  5. [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 ...

  6. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  7. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  8. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  9. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

随机推荐

  1. Mysql的二进制日志binlog的模式说明

    binlog模式总共可分为以下三种:row,statement,mixed 1.Row日志中会记录成每一行数据被修改的形式,然后在slave端再对相同的数据进行修改,只记录要修改的数据,只有value ...

  2. iOS:CYLTabBarController【低耦合集成TabBarController】

    导航 与其他自定义TabBarController的区别 集成后的效果 项目结构 使用CYLTabBarController 第一步:使用CocoaPods导入CYLTabBarController ...

  3. JQuery阻止事件冒泡---阻止后续代码执行

    (1)什么是事件起泡 首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什么要传播呢 ...

  4. Spring JDBCTemplate使用JNDI数据源

    xml配置: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverMana ...

  5. ASP.NET Word/Excel 权限问题

    在部署Word/Excel到服务器的时候,经常会碰到权限问题.例如;   Retrieving the COM class factory for component with CLSID {0002 ...

  6. [BZOJ3875][AHOI2014]骑士游戏(松弛操作)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3875 分析: 类似于spfa求最短路,设d[i]表示完全消灭i号怪物的最小花费,我们对 ...

  7. c#自动关闭 MessageBox 弹出的窗口

    我们都知道,MessageBox弹出的窗口是模式窗口,模式窗口会自动阻塞父线程的.所以如果有以下代码: MessageBox.Show("内容',"标题"); 则只有关闭 ...

  8. C#套接字和windowsAPI套接字

    C#服务器端 第一步:用指定的端口号和服务器的ip建立一个EndPoint对像:第二步:建立一个Socket对像:第三步:用socket对像的Bind()方法绑定EndPoint:第四步:用socke ...

  9. Unix 复制文件至指定目录

    cp /gaps/log/20160504/bxdx_20160504.log.Z   /home 将/gaps/log/20160504/bxdx_20160504.log.Z 文件复制到home路 ...

  10. <button>属性,居然才发现

    今天学习了一个表单验证的程序,发现点了一个<botton>之后,表单里面的所有输入框的内容,统统都消失了,后来一查看源代码,我发现居然是<botton>里面的属性如下: < ...