給出一個數(shù)字list,問其中全0子串有多少
不曉得為啥這樣的簡單數(shù)學(xué)題是Medium?假設(shè)有連續(xù)的k個0,那么可以生成1+2+...+k個全0子串,簡單等差數(shù)列求和
1 #2348
2 #Runtime: 865 ms (Beats 80.65%)
3 #Memory: 22.8 MB (Beats 61.29%)
4
5 class Solution(object):
6 def zeroFilledSubarray(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 ans, t = 0, 0
12 for i in range(len(nums)):
13 if nums[i] != 0:
14 t = 0
15 else:
16 t += 1
17 ans += t
18 return ans