Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5916c62

Browse filesBrowse files
author
王俊超
committed
commit
1 parent f415e9c commit 5916c62
Copy full SHA for 5916c62

File tree

Expand file treeCollapse file tree

2 files changed

+43
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-0
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+42Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.