英文:
error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean)
问题
///**获取所有加起来等于目标值的组合。错误:在Solution类型中,方法combinationSum(int[],int,List<Integer>)对于参数(int[],int,boolean)不适用**
import java.util.*;
public class Solution {
static List<Integer> b = new ArrayList<Integer>();
static List<List<Integer>> c = new ArrayList<List<Integer>>();
public static void combinationSum(int[] candidates, int target, List<Integer> b)
{
if(target == 0) {
c.add(b);
}
else {
for(int i = 0; i < candidates.length; i++) {
if(target > 0) {
List<Integer> newB = new ArrayList<Integer>(b);
newB.add(candidates[i]);
combinationSum(candidates, target - candidates[i], newB);
}
}
}
}
public static void main(String[] args) {
int[] candidates = {2, 3, 5};
int target = 8;
combinationSum(candidates, target, b);
System.out.println(c);
}
}
英文:
///to get all values that addup to the target . error The method combinationSum(int[], int, List<Integer>) in the type Solution is not
applicable for the arguments (int[], int, boolean)
import java.util.*;
public class Solution {
static List<Integer> b= new ArrayList<Integer>();
static List<List<Integer>> c= new ArrayList<List<Integer>>();
public static void combinationSum(int[] candidates, int target, List<Integer> b)
{
if(target==0)
{
c.add(b);
}
else {
for(int i=0;i<candidates.length;i++)
{
// else
// {
// if( target < 0 )
// {
//b.remove( b.size() - 1 );
// }
if(target>0)
{
//b.add(candidates[i]);
combinationSum(candidates,target-candidates[i],b.add(candidates[i]));
//b.remove( b.size() - 1 );
}
//}
}
}
//return;
}
public static void main(String[] args)
{
int[] candidates= {2,3,5};
int target=8;
combinationSum(candidates,target,b);
System.out.println(c);
}
答案1
得分: 1
你可以尝试这样做:
b.add(candidates[i]);
combinationSum(candidates,target-candidates[i],b));
首先将候选项添加到 b
中,然后在递归调用中传递这个列表。
英文:
You can try this :
b.add(candidates[i]);
combinationSum(candidates,target-candidates[i],b));
First add the candidates to b
and then pass the list in the recursive call.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论