Leetcode 221. Maximal Square
本题用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的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- (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 ...
- [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
问题描述: 题目链接: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 ...
随机推荐
- iOS关于html缓存
方式一:截取请求正则.图片缓存 实现webview缓存网页内容难点在缓存图片上.html代码的缓存相对简单,具体实现思路是这样的:第1步.先获取html页面里所有图片地址.方法一:离线获取获取到htm ...
- Java 日志性能优化
1. 选择合理的日志级别.合理控制日志内容 2. 控制日志的输出内容和格式 logger.debug("Entry number: " + i + " is " ...
- 搜索引擎关键词劫持之php篇(源码与分析)
摘要:其实原理很简单: 搜索引擎关键词劫持的过程实际上就是,修改肉鸡站点(webshell站点)A的首页(希望被搜索引擎收录的页面,一般情况下是首页),使之做出如下判断: if(来访者是蜘蛛){ 输出 ...
- WP老杨解迷:发布包多少大小合适
有位做安卓的老兄这样描述发布包大小问题:发布包和女人一样,新包如年轻女子,不能太瘦,太瘦没有货,所以大家都喜欢身段窈窕的少女,正火的产品如中年妇女,要得是风韵魅力,胖瘦已经不那么重要,但是也不能太胖, ...
- Windows 8.1 新增控件之 AppBar
Windows 8.1 与Windows 8 相比已经有了很多改进,从ITPro 角度这篇文章<What's New in Windows 8.1>已经表述的很详细.对开发者来说,最明显的 ...
- Integer.valueof(null)报错
原文 http://javacat360.iteye.com/blog/2024378 主题 Java 昨天,一同事问我一个问题,估计是他前段日子面试遇到的 问题很简单,String.valueof ...
- 建立mvc过程
1.public class dbContext:Dbcontext { private readonly static string CONNECTION_STRING="name=d ...
- Block Chain, a protocol view
我做了个区块链的文档,给自己扫盲用的,有兴趣的可以看下,主要是自己画示意图比较好理解,示意图之后的专题部分,内容直接取自参考链接.网上的资料都是谈区块链有什么性质.有什么能力.有什么应用之类的,我主要 ...
- 移动端调试利器 JSConsole 介绍
先看这篇文章 Web应用调试:现在是Weinre和JSConsole,最终会是WebKit的远程调试协议. 我们先不看未来,从此文可见,当下的移动端调试还是 Weinre 和 JSConsole 的天 ...
- .Net 程序集按需加载机制
在开始本文之前先提两个疑问: 1.一个.Net程序依赖很多的dll,那个他们是在应用程序启动的时候全部把所依赖的动态库全部都加载到应用程序域中的呢还是有选择的加载呢? 2.当应用程序已经启动后我们动态 ...