s******3 发帖数: 61 | 1 相对于I来说只是一个很小的修改就可以了,
不过发现现在过不了OJ,总是提示TLE,
大家帮忙看看是什么问题?
谢谢!
public List> combinationSum2(int[] candidates, int target) {
List list = new ArrayList();
List> res = new ArrayList>();
Arrays.sort(candidates);
dfs(candidates, target, 0, list, res);
return res;
}
public void dfs(int[] candidates, int target, int start,
List list, List> res) {
if (target < 0) return;
if (target == 0) {
res.add(new ArrayList(list));
return;
}
for (int i = start; i < candidates.length && candidates[i] <= target
; i++) {
if (i == start || candidates[i] != candidates[i-1]) {
list.add(candidates[i]);
dfs(candidates, target, i+1, list, res);
list.remove(list.size()-1);
}
}
} | g**u 发帖数: 583 | 2 Seems you did not update the target value here:
dfs(candidates, target, i+1, list, res);
--> dfs(candidates, target - candidates[i], i+1, list, res); | n******n 发帖数: 12088 | 3 怎么DFS都出来了
【在 s******3 的大作中提到】 : 相对于I来说只是一个很小的修改就可以了, : 不过发现现在过不了OJ,总是提示TLE, : 大家帮忙看看是什么问题? : 谢谢! : public List> combinationSum2(int[] candidates, int target) { : List list = new ArrayList(); : List> res = new ArrayList>(); : Arrays.sort(candidates); : dfs(candidates, target, 0, list, res); : return res;
|
|