Description
Given an array of integers and an integer k, return the total number of continuous subarrays whose sum equals k.
Examples
Input:
nums = [1,1,1], k = 2Output:
2Explanation:
Subarrays [1,1] at indices 0-1 and 1-2.
Input:
nums = [3,-3,1,1,1], k = 3Output:
3Explanation:
Three subarrays sum to 3: [3] at index 0 (3=3), [3,-3,1,1,1] from indices 0-4 (3-3+1+1+1=3), and [1,1,1] from indices 2-4 (1+1+1=3). The negative number -3 allows the full array to also sum to k despite containing more elements.
Input:
nums = [1], k = 1Output:
1Explanation:
Only one subarray [1] equals k=1. This tests the edge case of a single-element array where the element equals the target sum.
Constraints
- •
1 ≤ nums.length ≤ 2 * 10⁴ - •
-1000 ≤ nums[i] ≤ 1000