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 ...
随机推荐
- IntelliJ IDEA运行tomcat项目编码错误, 及如何指定tomcat编码
刚开始用IDEA, 在跑dubbo开发时, 发现一个很奇怪的问题, 远程调用服务端的方法时, 传入的中文参数会变成GBK编码. 经过好长时间的跟踪终于把问题定位到了IDEA里配置的Tomcat. 凡是 ...
- Razor 模板自己渲染出结果 string
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNet ...
- 前端见微知著JavaScript基础篇:你所不知道的apply, call 和 bind
在我的职业生涯中,很早就已经开始使用JavaScript进行项目开发了.但是一直都是把重心放在了后端开发方面,前端方面鲜有涉及.所以造成的一个现象就是:目前的前端知识水平,应付一般的项目已然是足够的, ...
- CAP原理的证明
CAP概述 C: Consistency 一致性 A: Availability 可用性 P:Partition Tolerance分区容错性 CAP理论的核心是:一个分布式系统不可能同时很好的满足一 ...
- 如何在Vue2中实现组件props双向绑定
Vue学习笔记-3 前言 Vue 2.x相比较Vue 1.x而言,升级变化除了实现了Virtual-Dom以外,给使用者最大不适就是移除的组件的props的双向绑定功能. 以往在Vue1.x中利用pr ...
- [C语言]一个很实用的服务端和客户端进行UDP通信的实例
前段时间发了个TCP通信的例子,现在再来一个UDP通信的例子.这些可以作为样本程序,用到开发中.“裸写”socket老是记不住步骤,经常被鄙视…… 下面的例子很简单,写一个UDP的server用于收包 ...
- 第十五章:输入和输出(I/O)
一:流分类 抽象基类:InputStream和Reader 抽象类不能用于创建模板哦! OutputStream和Writer 方向: 以内存为中心! 输入流(读) 输出流(写) 数据 ...
- 网页之间信息传递方式(Cookie,Session)
1.使用header()函数的重定向方式实现网页跳转. EXE:header("Location: http://www.example.com/"); 2.URL的GET ...
- 网络设计中需要考虑的时延latency差异
Jeff Dean提到不同数据访问方式latency差异 Numbers Everyone Should Know L1 cache reference 0.5 ns Branch mispredic ...
- Ubuntu 14.04 安装 JDK 8,ubuntu14.04
第一步,下载Linux版JDK 可以通过访问Oracle官网下载,或者直接通过命令行下载. lxh@ubuntu:~$ wget -c http://download.oracle.com/otn-p ...