【问题标题】:error java The method combinationSum(int[], int, List<Integer>) in the type Solution is not applicable for the arguments (int[], int, boolean)错误 java The method combinationSum(int[], int, List<Integer>) 类型解决方案不适用于参数(int[], int, boolean)
【发布时间】:2020-05-29 03:25:58
【问题描述】:

///获取与目标相加的所有值。 error 类型解决方案中的方法combinationSum(int[], int, List) 不是 适用于参数 (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);
    }

【问题讨论】:

  • 我会仔细检查 javadoc,ArrayList.add(E e) 返回一个布尔值。它在你的递归调用中。
  • 我需要为每个递归调用添加新值

标签: java


【解决方案1】:

你可以试试这个:

b.add(candidates[i]);
combinationSum(candidates,target-candidates[i],b));

首先将候选人添加到b,然后在递归调用中传递列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多