子数组总和包含JavaScript中的至少两个元素
问题
我们需要编写一个JavaScript函数,该函数将一个整数数组arr作为第一个参数,将一个整数目标作为第二个参数和第一个参数。我们的函数应检查是否存在大小不小于2的连续子数组,其总和为k的倍数,即,总和为n*k,其中n可以是任何整数。
如果存在,则返回true,否则返回false。
例如,如果函数的输入为-
const arr = [23, 2, 6, 4, 7]; const target = 6;
那么输出应该是-
const output = true;
输出说明:
因为[23、2、6、4、7]是大小为5的连续子数组,总和为42。
示例
为此的代码将是-
const arr = [23, 2, 6, 4, 7];
const target = 6;
const checkSubarraySum = (arr = [], target = 1) => {
let sum = 0
const hash = {}
hash[0] = -1;
for (let i = 0; i1) return true
} else {
hash[sum] = i
}
};
return false;
};
console.log(checkSubarraySum(arr, target)); 输出结果控制台中的输出将是-
true
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短