我有通過使用遞歸方法調查groupSum的代碼。 我不明白遞歸在這個例子中是如何工作的。我用調試,但仍然不明白。數量和的遞歸如何在java中工作?
public class test {
public boolean groupSum(int start, int[] nums, int target) {
if(target == 0)
return true;
if (start == nums.length)
return false;
if (groupSum(start+1, nums, target-nums[start])) // what is the meaning of this line ? can we change this line to make the code easier to understand ?
return true;
return groupSum(start+1, nums, target);
}
public static void main(String[] args) {
int x = 0;
int y [] = {2,4,8};
int k = 10;
test t = new test();
boolean result = t.groupSum(x,y,k);
System.out.println(result);
}
}
感謝
不說你發現混淆,它很難知道你不明白。使用調試器應該能夠提供幫助,而且使用越多,它就會有用。 (它帶有練習) –
http://codingbat.com/prob/p145416上有一個很好的對這個問題的處理。你使用相同的測試用例。你從那裏來過嗎?他們的解釋怎麼樣,你不明白? –
是的,我來自那裏..我沒有正確理解這一行'groupSum(start + 1,nums,target-nums [start])' –