我有這樣的情況下,對於輸入的工作原理:遞歸添加subet
{-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;
}
}
如果您告訴我們該方法應該完成什麼,這將有所幫助。 – ajb