老师,这段代码可以accept,但是only faster than 5.76% ,想不到什么优化方案了,老师有没有什么建议?
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return result;
}
Arrays.sort(candidates);
boolean hasAnswer = candidates[0] <= target;
if (!hasAnswer) {
return result;
}
for (int i = 0; i < candidates.length; i++) {
int selected = candidates[i];
if (selected > target) {
return result;
}
if (target == selected) {
List<Integer> oneAnswer = new ArrayList<>();
oneAnswer.add(target);
result.add(oneAnswer);
return result;
} else {
List<List<Integer>> lists = combinationSum(candidates, target - selected);
for (List<Integer> list : lists) {
list.add(selected);
Collections.sort(list);
if (!result.contains(list)) {
result.add(list);
}
}
}
}
return result;
}