请教一个JavaScript的算法题

有一组数字[0,1,2,3,4,5,6,7,8,9],现给出一个数字,比如3,
要求从该组数字中选出相加等于3的组合(相加的数字3个),如0+0+3=3,0+1+2,3个相加的数字不能相同(如1+1+1就不行),无顺序要求。
大概函数是这样 fun(数组,和值,3) ,得到的是组合的个数.
参数3是相加的数字个数,这里规定是3,如0+0+3,这是3个数字

胡子哥哥
浏览 224回答 3
3回答

素胚勾勒不出你

递归查找符合规则的元素集合:部分逻辑是建立在认为集合中所有元素都是正数的基础上(() => {&nbsp; var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]&nbsp; console.log(calc(arr, 3, 3)) // 0,1,2&nbsp; console.log(calc(arr, 3, 1)) // 3&nbsp; function calc (arr, total, count, feed = []) {&nbsp; &nbsp; if (count === 1) { // check&nbsp; &nbsp; &nbsp; return arr.includes(total) ? feed.concat(total) : null&nbsp; &nbsp; } else if (count > 1) { // remove big number&nbsp; &nbsp; &nbsp; arr = arr.filter(item => item < total)&nbsp; &nbsp; } else if (count === 0) { // maybe too large&nbsp; &nbsp; &nbsp; return total === 0 ? feed : null&nbsp; &nbsp; } else if (total < 0 || count < 0) { // too large&nbsp; &nbsp; &nbsp; return null&nbsp; &nbsp; }&nbsp; &nbsp; for (let [index, item] of Object.entries(arr)) {&nbsp; &nbsp; &nbsp; let result = calc([&nbsp; &nbsp; &nbsp; &nbsp; ...arr.slice(0, index),&nbsp; &nbsp; &nbsp; &nbsp; ...arr.slice(index + 1)&nbsp; &nbsp; &nbsp; ], total - item, count - 1, feed.concat(item))&nbsp; &nbsp; &nbsp; if (result) return result&nbsp; &nbsp; }&nbsp; &nbsp; return null&nbsp; }})()

幕布斯7119047

请参考以下 python 代码实现# -*- coding: utf-8 -*-"""author: 李毅"""from unittest import TestCasedef permutation(array, nsum):&nbsp; &nbsp; ''' 假设数组元素不重复。 '''&nbsp; &nbsp; # 排序(升序)&nbsp; &nbsp; sarray = sorted(array)&nbsp; &nbsp; # 找出最大下标&nbsp; &nbsp; max_idx = len(sarray)&nbsp; &nbsp; for i, e in enumerate(sarray):&nbsp; &nbsp; &nbsp; &nbsp; if e > nsum:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_idx = i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; # 穷举&nbsp; &nbsp; result = []&nbsp; &nbsp; for i in range(max_idx):&nbsp; &nbsp; &nbsp; &nbsp; for j in range(i, max_idx):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k in range(j, max_idx):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i == j and j == k:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if sarray[i] + sarray[j] + sarray[k] == nsum:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.append((sarray[i], sarray[j], sarray[k]))&nbsp; &nbsp; return resultclass Test(TestCase):&nbsp; &nbsp; """ 单元测试 """&nbsp; &nbsp; def test_permutation(self):&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permutation(range(10), 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(0, 0, 3), (0, 1, 2)])&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permutation(range(10), 2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(0, 0, 2), (0, 1, 1)])&nbsp; &nbsp; &nbsp; &nbsp; # 边界值&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permutation(range(3), 3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(0, 1, 2)])&nbsp; &nbsp; &nbsp; &nbsp; self.assertEqual(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permutation(range(1, 4), 4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [(1, 1, 2)])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript