/// to get all values that addup to the target. error The method combinationSum(int[], int, List) 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);
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.