zoukankan      html  css  js  c++  java
  • 221. Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

    Example:

    Input: 
    
    1 0 1 0 0
    1 0 1 1 1
    1 1 1 1 1
    1 0 0 1 0
    
    Output: 4

    class Solution(object):
        def maximalSquare(self, matrix):
            """
            :type matrix: List[List[str]]
            :rtype: int
            """
            if matrix == []:
                return 0
            m = len(matrix)
            n = len(matrix[0])
            
            maxedge = 0
            
            for i in range(m):
                for j in range(n):
                    matrix[i][j] = int(matrix[i][j])
                    if matrix[i][j] and i and j:
                        matrix[i][j] = min(matrix[i][j-1], matrix[i-1][j], matrix[i-1][j-1]) + 1
                    maxedge = max(maxedge, matrix[i][j])
                    
            return maxedge **2
            
  • 相关阅读:
    LeetCode 169
    LeetCode 152
    LeetCode 238
    LeetCode 42
    LeetCode 11
    GDB基本调试
    小咪买东西(最大化平均值)
    codeforces 903D
    hdu 5883
    hdu 5874
  • 原文地址:https://www.cnblogs.com/boluo007/p/12541263.html
Copyright © 2011-2022 走看看