:type A: List[int] :type S: int :rtype: int
(self, A, S)
| 29 | """ |
| 30 | class Solution(object): |
| 31 | def numSubarraysWithSum(self, A, S): |
| 32 | """ |
| 33 | :type A: List[int] |
| 34 | :type S: int |
| 35 | :rtype: int |
| 36 | """ |
| 37 | |
| 38 | dicts = {0:1} |
| 39 | |
| 40 | pre = 0 |
| 41 | result = 0 |
| 42 | |
| 43 | for i in A: |
| 44 | pre += i |
| 45 | |
| 46 | result += dicts.get(pre-S, 0) |
| 47 | dicts[pre] = dicts.get(pre, 0) + 1 |
| 48 | |
| 49 | return result |