Skip to content

Commit 5916c62

Browse files
author
王俊超
committed
commit
1 parent f415e9c commit 5916c62

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Arrays;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
5+
/**
6+
* @author: wangjunchao(王俊超)
7+
* @time: 2018-09-28 15:15
8+
**/
9+
public class Solution {
10+
public List<List<Integer>> combinationSum2(int[] nums, int target) {
11+
List<List<Integer>> list = new LinkedList<>();
12+
Arrays.sort(nums);
13+
backtrack(list, new LinkedList<>(), nums, target, 0);
14+
return list;
15+
16+
}
17+
18+
/**
19+
* @param list 结果集
20+
* @param tempList 临时结果集
21+
* @param nums 可选值数组
22+
* @param remain 剩余值
23+
* @param start 可选值的起始下标
24+
*/
25+
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int remain, int start) {
26+
if (remain < 0) {
27+
return;
28+
} else if (remain == 0) {
29+
list.add(new LinkedList<>(tempList));
30+
} else {
31+
for (int i = start; i < nums.length; i++) {
32+
if (i > start && nums[i] == nums[i - 1]) {
33+
continue;
34+
}
35+
tempList.add(nums[i]);
36+
// 数值不可以被重用
37+
backtrack(list, tempList, nums, remain - nums[i], i + 1);
38+
tempList.remove(tempList.size() - 1);
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)