error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean)

huangapple go评论130阅读模式
英文:

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&lt;Integer&gt; b= new ArrayList&lt;Integer&gt;();
	static List&lt;List&lt;Integer&gt;&gt; c= new ArrayList&lt;List&lt;Integer&gt;&gt;();
	public static void combinationSum(int[] candidates, int target, List&lt;Integer&gt; b) 
	{	
		if(target==0)
    	{
    		c.add(b);
    	}
		
		else {
        for(int i=0;i&lt;candidates.length;i++)
        {	
        	
//        	else
//        	{
          //  if( target &lt; 0 )
          //  {
            	//b.remove( b.size() - 1 );
          //  }
        	if(target&gt;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.

huangapple
  • 本文由 发表于 2020年5月29日 11:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定