java任意个数的数相加_js 无序数组 任意个数 相加之和为定量m?

不知道效率怎么样

实现了再说吧~~

function getcombinationAndMatched(initval,array,targetValue,someGroup){

var result=[];

var matched=[];

var startIndex;

if(someGroup.index.length===0){

startIndex=-1;

}else{

startIndex=someGroup.index[someGroup.index.length-1];

}

for(var i=startIndex+1;i

if(initval+array[i]<=targetValue) {

if(initval+array[i]===targetValue){

matched.push(someGroup.index.slice(0).concat([i]));

someGroup.value=targetValue;

someGroup.index.push(i);

}else{

result.push({

value:initval+array[i],

index:someGroup.index.slice(0).concat([i])

});

}

}else{

break;

}

}

return {

matched:matched,

result:result

}

}

function getMatchedGroup(groupArray,orgArray,targetValue){

var matched=[];

for(var q=0;q

var groupResult=getcombinationAndMatched(groupArray[q].value,orgArray,targetValue,groupArray[q]);

matched=matched.concat(groupResult.matched);

if(groupResult.result.length!=0){

var _matched_=getMatchedGroup(groupResult.result,orgArray,targetValue);

matched=matched.concat(_matched_);

}

}

return matched;

}

var orgArray=[1,2,3,4,5,6,7,8,9,20,30];

var targetValue=41;

orgArray.sort(function(a, b) {

return a - b;

});

var subArray=[];

var findIndex=orgArray.findIndex(function(item){

return item>targetValue;

});

if(findIndex===-1){

subArray=orgArray;

}else{

subArray=orgArray.slice(0,findIndex);

}

var result=getMatchedGroup([{

value:0,

index:[]

}],subArray,targetValue);

//console.log(result);

result.forEach(function(item){

console.log(item.map(function(valueIndex){

return orgArray[valueIndex];

}).join(','));

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可以使用回溯法(Backtracking)来解决。回溯法是一种暴力搜索的算法,它通过不断尝试所有可能的情况来寻找问题的解。 在这个问题中,我们可以使用一个递归函来进行回溯搜索。函需要接收以下参: - 数组arr:要搜索的数组 - int target:目标和 - int start:当前搜索的起始下标 - List<Integer> path:保存当前搜索路径的列表 - List<List<Integer>> result:保存所有搜索结果的列表 具体实现如下: ```java public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { path.add(arr[i]); // 将当前加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前从路径中移除 } } ``` 在上面的函中,当目标和为0时,说明找到了一组解,将当前路径保存到结果列表中。如果目标和小于0或者已经搜索到数组末尾,直接返回。 在每次搜索时,我们从当前位置开始,依次将数组中的加入路径中。然后递归搜索下一层,将目标和减去当前。当搜索返回时,我们需要回溯,将当前从路径中移除,以便尝试其他可能的组合。 下面是完整的代码: ```java import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { int[] arr = {2, 3, 6, 7}; int target = 7; List<List<Integer>> result = combinationSum(arr, target); System.out.println(result); } public static List<List<Integer>> combinationSum(int[] arr, int target) { Arrays.sort(arr); // 先对数组进行排序,以便剪枝 List<List<Integer>> result = new ArrayList<>(); findSum(arr, target, 0, new ArrayList<>(), result); return result; } public static void findSum(int[] arr, int target, int start, List<Integer> path, List<List<Integer>> result) { if (target == 0) { // 如果目标和为0,说明找到了一组解 result.add(new ArrayList<>(path)); return; } if (target < 0 || start == arr.length) { // 如果目标和小于0或者已经搜索到数组末尾,直接返回 return; } for (int i = start; i < arr.length; i++) { if (i > start && arr[i] == arr[i-1]) continue; // 剪枝,去掉重复的 path.add(arr[i]); // 将当前加入路径 findSum(arr, target - arr[i], i + 1, path, result); // 递归搜索 path.remove(path.size() - 1); // 回溯,将当前从路径中移除 } } } ``` 在上面的代码中,我们先对数组进行排序,以便剪枝,去掉重复的。在搜索过程中,如果发现当前和前一个数相同,直接跳过,避免重复计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值