在一个由 '0''1' 组成的二维矩阵内,找到只包含 '1' 的最大正方形,并返回其面积。

例:

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximal-square

解析:

动态规划,dp数组,边界情况为1直接填1,否则填0。其他位置,因为是正方形,所以左边和上边的值中短的一条边加上左上角使我们的判断条件,因为我们求的是右下角的位置。知道这个规则就可以填写dp数组了。

class Solution(object):def maximalSquare(self, matrix):""":type matrix: List[List[str]]:rtype: int"""if len(matrix) == 0 or len(matrix[0]) == 0:  # 判空return 0maxSide = 0  # 最长边的长度rows, columns = len(matrix), len(matrix[0])  # 获取长和宽dp = [[0] * columns for _ in range(rows)]  # 初始化dp数组for i in range(rows):  # 遍历for j in range(columns):if matrix[i][j] == '1':  # 有值才有正方形if i == 0 or j == 0:  # 边界情况dp[i][j] = 1else:dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1  # 主要判断方法maxSide = max(maxSide, dp[i][j])  # 去最大值maxSquare = maxSide * maxSide  # 求面积return maxSquare

最大正方形Python解法相关推荐

  1. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  2. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

  3. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  4. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  5. LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法

    题目地址:Poor Pigs - LeetCode There are 1000 buckets, one and only one of them is poisonous, while the r ...

  6. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  7. LeetCode 148. Sort List--面试算法题--C++,Python解法

    LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...

  8. LeetCode 20. Valid Parentheses--笔试题--Python解法

    题目地址:Valid Parentheses - LeetCode Given a string containing just the characters '(', ')', '{', '}', ...

  9. LeetCode 145. Binary Tree Postorder Traversal--后序遍历--先序遍历反向输出--递归,迭代--C++,Python解法

    题目地址:Binary Tree Postorder Traversal - LeetCode Given a binary tree, return the postorder traversal ...

最新文章

  1. springboot配置国际化资源文件 使用themself模板进行解析
  2. SafeSEH原理与对抗
  3. android linux应用安装位置,Android中App安装位置详解
  4. 详解/etc/fstab文件内容
  5. [ARM异常]-ARMV8的中断的routing和Mask表
  6. wxWidgets:wxOwnerDrawnComboBox类用法
  7. selenium,webdriver 执行js语句 对象是百度
  8. 常见人名大全_生辰八字起名取名:2020年属鼠的女孩起名字大全
  9. LINQ能不能用系列(一)LINQ to Object 效率比对
  10. [Ajax] Ajax的基本用法
  11. C#读取XML文件的基类实现
  12. SparkStreaming 入门案例之wordcount
  13. Kotlin实战【六】Kotlin中集合的创建
  14. office向快速访问工具栏加快速操作
  15. T61|NV显卡门|根据售后维修部数据显示爆发期来临|预防显卡门|解决显卡门
  16. Appium工具使用教程
  17. SLA是什么?SLA光固化3D打印机原理是什么
  18. c语言点阵输出字母,单片机C语言程序设计:8X8LED 点阵显示数字
  19. 京东、闲鱼、转转的二手战场
  20. Vue 实现简单的时间轴 时间进度条

热门文章

  1. SAP UI5 初学者教程之二十八 - SAP UI5 应用的集成测试工具 OPA 介绍试读版
  2. 如何给基于 SAP Cloud SDK 的应用增添缓存支持 Cache support
  3. SAP Spartacus PageMetaResolver 的单元测试
  4. TypeScript 的 Object Types
  5. SAP UI渲染模式:客户端渲染 VS 服务器端渲染
  6. 网友提问:关于CX_VSI_SYSTEM_ERROR异常,Fiori病毒扫描参数文件
  7. 打印SAP Spartacus generic link指向的url
  8. SAP Spartacus里如何禁掉默认的css style
  9. SAP OData服务性能测量的几种工具和手段
  10. 那些年陪我走过一个又一个加班夜晚的程序员鼓励师们