考慮的源代碼:如何提高算法總和?
public class subSetSumR {
// Solving Subset sum using recursion
// Programmed by Fuentes, Feb. 9, 2009
// Subset sum consists of finding a subset of mySet whose elements add up to goal
// It is a well-known NP-complete problem
public static boolean subSetSumRecur (int[] mySet, int n, int goal) {
if (goal == 0) return true;
if ((goal < 0) | (n >= mySet.length)) return false;
if (subSetSumRecur(mySet, n + 1, goal - mySet[n])) {
System.out.print(mySet[n] + " ");
return true;
}
if (subSetSumRecur(mySet, n + 1, goal)) return true;
return false;
}
}
重要的事實:輸入的數量大於1我如何利用這一點來加快上述解決方案?
可能更適合[CodeReview](http://codereview.stackexchange.com/)。 –
這是功課嗎? –
如果變量'goal'是你的輸入,那麼可以簡單地刪除幾個語句。 – usr2564301