-
Notifications
You must be signed in to change notification settings - Fork 481
/
0930.py
35 lines (31 loc) · 863 Bytes
/
0930.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution:
def numSubarraysWithSum(self, A, S):
"""
:type A: List[int]
:type S: int
:rtype: int
"""
zeros, result, cnt = list(), 0, 0
if not A:
return result
for num in A:
if num:
zeros.append(cnt)
cnt = 0
else:
cnt += 1
zeros.append(cnt)
if S:
for i in range(len(zeros) - S):
result += (zeros[i] + 1)*(zeros[i+S] + 1)
return result
if S == 0:
for i in range(len(zeros)):
if zeros[i] > 0:
result += (zeros[i] + 1)*zeros[i]
result //= 2
return result
if __name__ == "__main__":
A = [0,0,0,0,0]
S = 0
print(Solution().numSubarraysWithSum(A, S))