python leetcode 日记--Maximal Square--221
题目:
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.
本题为一个典型的动态规划问题,因此可以使用动态规划的思想进行。
step1,初始化原始行和列
step2,构建存储结果矩阵(其大小与原始问题空间一样)
step3,对结果矩阵进行遍历找出最优解
其中,step2是动态规划的核心,其主要思想为将整个问题划分为若干个子问题,对于每个子问题,都可以借助其他子问题的解来得到自己的解;
那么对应到问题中核心思想如何理解呢?
| 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
如上这个4*4表格,首先我们假定是有当前位置相左上方画正方形,那么(0,0)位置开始,初始化时仅看第一行第一列,因为是边界,因此如果值等于1,那么表示可以画出一个1单位长度的正方形满足题意。若等于0,则表示没有满足的正方形。根据此原则,结果矩阵第一行为(1,1,1,1)第一列为(1,1,0,1),然后从(1,1)位置开始遍历剩余的位置。那么怎么划分子问题呢?其实划分子问题已经开始了,比如现在在(1,1)位置,因为此位置已经为1,如果(1,1)左上方的三个位置都为的话,那么对于(1,1)这个位置的对应的结果矩阵就可以为2,而要知道剩下三个位置是否为1,只需要查那三个位置的结果矩阵即可,因为结果矩阵纪录了能够画出多少个单位长度的正方形,根据这个原理,只要找到三个中的最小值,在加上自己的这个位置,就可以得到结果。这样遍历下来,结果矩阵就纪录了从此点向左上方画正方形所满足题意的最大值。
本人的实现代码如下:
class Solution(object):
def maximalSquare(self, matrix):
if not matrix:
return 0
lines=len(matrix)
lists=len(matrix[0])
mat=[[0]*lists for i in range(lines)]
#初始化
for i in range(lists):
mat[0][i]=int(matrix[0][i])
for i in xrange(lines):
mat[i][0]=int(matrix[i][0])
result=0
#构造结果矩阵
for i in xrange(1,lines):
for j in xrange(1,lists):
mat[i][j]=int(matrix[i][j])
if mat[i][j] is not 0:
mat[i][j]=(min(mat[i-1][j-1],mat[i][j-1],mat[i-1][j])+1)
result=0
#遍历结果矩阵得到结果
for i in mat:
for j in i:
if result<j:
result=j
return result**2
python leetcode 日记--Maximal Square--221的更多相关文章
- python leetcode 日记 --Contains Duplicate II --219
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- leetcode之Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 【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 - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
随机推荐
- CSS3:transform translate transition 这些都是什么?
transform:一个属性名称,即CSS3 2D转换 属性. translate:一个属性函数,用法是translate(dx,dy) div { transform: translate(50px ...
- 思科Cisco 2960系列交换机配置命令
配置密码: 2960>en :第一次密码为空 2960h#conf t :进入全局配置模式 2960(config)#hostname swa :设置交换机名 2960(config)#enab ...
- Linux 概念架构的理解
摘要 Linux kernel 成功的两个原因: 架构设计支持大量的志愿开发者加入到开发过程中: 每个子系统,尤其是那些需要改进的,都支持很好的扩展性. 正是这两个原因使得 Linux kernel ...
- 自定义Dialog
功能:从底部弹出的对话框,加入动画 步骤:1 定义dialog布局文件 2 设置标题,透明度style.xml,选择器selector.xml ,圆角shape.xml 等样式文件 3 设置显示位置, ...
- IntelliJ IDEA中使用综合使用Maven和Struts2
在Intellij IDEA中手动使用Maven创建Web项目并引入Struts2 创建一个新的Maven项目 建好项目之后点击左下角的enable auto import 项目部署 在Moudule ...
- php新手常用的函数(随时更新)
//数字保留两位小数 $n = sprintf("%1.2f", $n); //方法二 $n = number_format($n, 2, '.', ''); //UTF8转GBK ...
- day5 -指针
指针和指针变量 指针就是地址,地址就是指针 地址就是存放单元的编号 指针变量是存放地址的变量 指针和指针变量是两个不同的概念,但是要注意,通常我们叙述时会把指针变量简称为指针,实际他们含义并不一样 指 ...
- C中的volatile用法
.volatile的本质: 1> 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值 ...
- applicationContext.xml简单笔记
applicationContext.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- Python之路 day2 购物车小程序1
#Author:ersa ''' 程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时 ...