查找集合中有多少子集具有目标总和

时间:2014-02-13 01:39:09

标签: java

给定一个int s数组和一个目标值,找出数组中有多少子集添加到目标值。

当我运行它时,我一直得到时髦的数字......有什么不对吗?

public int findGoal(int[] anArray,int index, int current, int goal, String result){

    //Base case: At the end of the array
    if (anArray.length <= index || current>goal) {
        return count;
    }

    //Recursive case: If not at end iterate
    for (int i = index; i < anArray.length; i++) {          
        if (current + anArray[i] == goal)   {
            count++;
        } else if (current + anArray[i] < goal) {
            count = findGoal(anArray, i + 1, current + anArray[i], goal, result + " " + anArray[i]);
        }               
    }

    return count;
}

0 个答案:

没有答案