← 返回 google 的题目列表Subarray Sum Equals K (with streaming-data follow-up)
类型:online_judge
Problem: Subarray Sum Equals K (with Streaming Data follow-up)
Given an integer array nums and an integer k, compute the number of continuous subarrays whose sum equals k.
Requirements
Return the count of continuous subarrays whose sum is exactly k.
Aim for an efficient time complexity.
Follow-up (Streaming Data)
If nums is not provided all at once but arrives as a stream: each time a new element x arrives, you need to update (or output) the number of qualifying subarrays seen so far. Explain how to support incremental updates.
Constraints (suggested)
1 <= len(nums) <= 2 * 10^5
-10^4 <= nums[i] <= 10^4
-10^7 <= k <= 10^7
Examples
Input: nums = [1,1,1], k = 2 Output: 2
Input: nums = [1,2,3], k = 3 Output: 2
Input: nums = [3,4,7,2,-3,1,4,2], k = 7 Output: 4
Input: nums = [1,-1,0], k = 0 Output: 3
Input: nums = [-1,-1,1], k = 0 Output: 1
Example
Input
3 2
1 1 1
Output
2