【发布时间】:2014-03-03 00:51:56
【问题描述】:
我有一个适用于输入的案例:
{-5,0,5}, 2, 0 // which correctly evaluates to true
{-5,0,5}, 3, 4 // which correctly evaluates to false
{-5,0,5}, 3, 0 // which correctly evaluates to true
但有输入:
{6,5,6}, 2, 12 // says false when true
它没有得到正确的布尔值... 有人可以帮助调试问题吗?
public static boolean subset(int[] array, int n, int target) {
for (int i = 0; i < array.length; i++) {
int[] list = new int[array.length - 1];
for (int j = 0; j < array.length - 1; j++) {
list[j] = array[j+1];
}
subset(list, n, target);
}
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
if (sum == target) {
return true;
}
else {
return false;
}
}
【问题讨论】:
-
如果您告诉我们该方法应该完成什么,将会有所帮助。
标签: java recursion combinations