We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 中等
Related Topics: 数组, 哈希表, 前缀和
给你一个整数数组 nums 和一个整数 k ,请你统计并返回 _该数组中和为 k的连续子数组的个数 _。
nums
k
示例 1:
输入:nums = [1,1,1], k = 2 输出:2
示例 2:
输入:nums = [1,2,3], k = 3 输出:2
提示:
-1000 <= nums[i] <= 1000
Language: JavaScript
/** * @param {number[]} nums * @param {number} k * @return {number} */ // 前缀和 + 哈希表优化 var subarraySum = function(nums, k) { const mp = new Map(); mp.set(0, 1); let count = 0, pre = 0; for (const x of nums) { pre += x; if (mp.has(pre - k)) { count += mp.get(pre - k); } if (mp.has(pre)) { mp.set(pre, mp.get(pre) + 1); } else { mp.set(pre, 1); } } return count; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
560. 和为 K 的子数组
Description
Difficulty: 中等
Related Topics: 数组, 哈希表, 前缀和
给你一个整数数组
nums
和一个整数k
,请你统计并返回 _该数组中和为k
的连续子数组的个数 _。示例 1:
示例 2:
提示:
-1000 <= nums[i] <= 1000
Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: