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行不同。

posted on 2019-12-01 12:14  Sempron2800+  阅读(160)  评论(0编辑  收藏  举报