I was doing a leetcode problem and while reviewing a solution I was quite dumbfounded by a certain line in the solution. The leetcode problem in particular is https://leetcode.com/problems/binary-subarrays-with-sum/
The solution:
class Solution:
def numSubarraysWithSum(self, A: List[int], S: int) -> int:
res = 0
sm = 0
sums = collections.defaultdict(int)
for a in A:
sm += a
res += sums[sm - S] + (sm == S)
sums[sm] += 1
return res
I understand everything that is going on besides this line:
res += sums[sm - S] + (sm == S)
I have never seen a conditional in an addition operation before.