Subarray Sum Equals K
Count the contiguous, non-empty stretches of nums whose items add up to exactly k.
Stretches at different positions count separately, even if they hold the same numbers.
Example 1
nums [1,1,1]
k 2
returns 2
Two stretches: indices 0 to 1 and indices 1 to 2.
Example 2
nums [1,2,3]
k 3
returns 2
The stretches [1, 2] and [3].
Constraints
1 <= len(nums) <= 30-20 <= nums[i] <= 20-50 <= k <= 50
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.