leetcode1277

时间:2019-12-01 13:48:21   收藏:0   阅读:111
 1 class Solution:
 2     def countSquares(self, matrix: List[List[int]]) -> int:
 3         m = len(matrix)
 4         if m == 0:
 5             return 0
 6         n = len(matrix[0])
 7         dp = [[0 for _ in range(n+1)]for _ in range(m+1)]
 8         res = 0
 9         for i in range(m):
10             for j in range(n):
11                 if matrix[i][j] == 1:
12                     dp[i+1][j+1] = min(min(dp[i][j+1],dp[i+1][j]),dp[i][j]) + 1
13                     res += dp[i+1][j+1]
14         return res

和题目leetcode221思路一样,只有第13行和14行不同。

原文:https://www.cnblogs.com/asenyang/p/11965584.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!