class Solution:
def numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:
#求list的子list个数,子list的和=k,利用前缀和求解
def sum(nums,k):
count=0
pre=0
mp=Counter([0])
for i in range(len(nums)):
pre+=nums[i]
if pre-k in mp:
count+=mp[pre-k]
mp[pre]+=1
return count
re=0
for m in range(len(matrix)):
total=[0]*len(matrix[0])
for n in range(m,len(matrix)):
for c in range(len(matrix[0])):
total[c]+=matrix[n][c]
re+=sum(total,target)
return re
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/power-of-four/solution/4de-mi-by-leetcode-solution-b3ya/
来源:力扣(LeetCode)