给定一个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;
}