题目:

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. 1 0 1 0 0
  2. 1 0 1 1 1
  3. 1 1 1 1 1
  4. 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,只需要查那三个位置的结果矩阵即可,因为结果矩阵纪录了能够画出多少个单位长度的正方形,根据这个原理,只要找到三个中的最小值,在加上自己的这个位置,就可以得到结果。这样遍历下来,结果矩阵就纪录了从此点向左上方画正方形所满足题意的最大值

本人的实现代码如下:

  1. class Solution(object):
  2. def maximalSquare(self, matrix):
  3. if not matrix:
  4. return 0
  5. lines=len(matrix)
  6. lists=len(matrix[0])
  7. mat=[[0]*lists for i in range(lines)]
    #初始化
  8. for i in range(lists):
  9. mat[0][i]=int(matrix[0][i])
  10. for i in xrange(lines):
  11. mat[i][0]=int(matrix[i][0])
  12. result=0
    #构造结果矩阵
  13. for i in xrange(1,lines):
  14. for j in xrange(1,lists):
  15. mat[i][j]=int(matrix[i][j])
  16. if mat[i][j] is not 0:
  17. mat[i][j]=(min(mat[i-1][j-1],mat[i][j-1],mat[i-1][j])+1)
  18. result=0
    #遍历结果矩阵得到结果
  19. for i in mat:
  20. for j in i:
  21. if result<j:
  22. result=j
  23. return result**2

python leetcode 日记--Maximal Square--221的更多相关文章

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

  2. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

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

  4. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

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

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

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

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

  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 - Maximal Square

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

随机推荐

  1. js中的undefined与null、空值的比较

    最近在修改一个项目,总是报Js错误: 无法获取属性“length”的值: 对象为 null 或未定义 点开调试之后,惊奇的发现markerArr的值是undefined 所以我就将代码改成如下形式: ...

  2. 使用boost的asio,io_service无法初始化

    今天用vs编一个用asio写的程序,发现在tcp::acceptor::open()失败,查了好久,发现是acceptor绑定的io_service没有正确的初始化,又查了半天,发现是需要加一个预编译 ...

  3. Mac下安装LNMP(Nginx+PHP5.6)环境

    [转自:http://avnpc.com/pages/install-lnmp-on-osx] 安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10.1)为例, ...

  4. Java 泛型和通配符解惑

    转自:http://www.linuxidc.com/Linux/2013-10/90928.htm T  有类型 ?  未知类型 一.通配符的上界 既然知道List<Cat>并不是Lis ...

  5. ajax用法流程

    这里是用javascript做的一个ajax的一个用法以及总结概括.供友友们进行参考. 1 window.onload=function() { var oBtn=document.getElemen ...

  6. zigbee学习之路(二)点亮LED

    一.前言 今天,我来教大家如何点亮led,这也是学习开发板最基础的步骤了. 二.原理分析 cc2530芯片跟虽然是51的内核,但是它跟51单片机还是有区别的,51单片机不需要对IO口进行配置,而cc2 ...

  7. Linux phpbb论坛的安装(中文版)

    1:建立文件夹

  8. src url href uri的区别和联系

  9. 避免jsp传参返回乱码问题

    $("#searchForm input").each(function(i){ var obj=$(this); var va=obj.val(); obj.val(decode ...

  10. WebSocket实战之————Workerman服务器的安装启动

    安装php apt-get install php5-cli root@iZ23b64pe35Z:/home/www# php -v PHP 5.5.9-1ubuntu4.20 (cli) (buil ...