Skip to content

Commit 1e61b1a

Browse files
committed
40. Combination Sum II
1 parent c11c82a commit 1e61b1a

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/leetcode/_40_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._40_;
2+
3+
4+
/**
5+
* Created by zhangbo54 on 2019-03-04.
6+
*/
7+
public class Main {
8+
public static void main(String[] args) {
9+
Solution solution = new Solution();
10+
System.out.println(solution.combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 10));
11+
}
12+
}
13+

src/leetcode/_40_/Solution.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode._40_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
import java.util.stream.Collectors;
11+
12+
class Solution {
13+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
14+
Arrays.sort(candidates);
15+
Set<List<Integer>> result = new HashSet<>();
16+
for (int i = 0; i < candidates.length; i++) {
17+
if (i > 1 && candidates[i] == candidates[i - 1]) {
18+
continue;
19+
}
20+
if (target == candidates[i]) {
21+
result.add(Collections.singletonList(target));
22+
}
23+
for (List<Integer> list : this.combinationSum2(candidates, i, target - candidates[i])) {
24+
list.add(candidates[i]);
25+
result.add(list);
26+
}
27+
}
28+
return new ArrayList<>(result);
29+
}
30+
31+
private List<List<Integer>> combinationSum2(int[] candidates, int index, int target) {
32+
List<List<Integer>> result = new ArrayList<>();
33+
if (target < candidates[index]) {
34+
return result;
35+
}
36+
if (Arrays.binarySearch(candidates, index + 1, candidates.length, target) > 0) {
37+
result.add(new ArrayList<>(Collections.singletonList(target)));
38+
}
39+
for (int i = index + 1; i < candidates.length; i++) {
40+
for (List<Integer> list : this.combinationSum2(candidates, i, target - candidates[i])) {
41+
list.add(candidates[i]);
42+
result.add(list);
43+
}
44+
}
45+
return result;
46+
}
47+
}

src/leetcode/_40_/solution.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
### [40\. Combination Sum IICopy for MarkdownCopy for MarkdownCopy for Markdown](https://leetcode.com/problems/combination-sum-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a collection of candidate numbers (`candidates`) and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sums to `target`.
7+
8+
Each number in `candidates` may only be used **once** in the combination.
9+
10+
**Note:**
11+
12+
* All numbers (including `target`) will be positive integers.
13+
* The solution set must not contain duplicate combinations.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: candidates = [10,1,2,7,6,1,5], target = 8,
19+
A solution set is:
20+
[
21+
[1, 7],
22+
[1, 2, 5],
23+
[2, 6],
24+
[1, 1, 6]
25+
]
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: candidates = [2,5,2,1,2], target = 5,
32+
A solution set is:
33+
[
34+
  [1,2,2],
35+
  [5]
36+
]
37+
```
38+
39+
40+
#### Solution
41+
42+
Language: **Java**
43+
44+
```java
45+
class Solution {
46+
   public List<List<Integer>> combinationSum2(int[] candidates, int target) {
47+
       Arrays.sort(candidates);
48+
       Set<List<Integer>> result = new HashSet<>();
49+
       for (int i = 0; i < candidates.length; i++) {
50+
           if (i > 1 && candidates[i] == candidates[i - 1]) {
51+
               continue;
52+
          }
53+
           if (target == candidates[i]) {
54+
               result.add(Collections.singletonList(target));
55+
          }
56+
           for (List<Integer> list : this.combinationSum2(candidates, i, target - candidates[i])) {
57+
               list.add(candidates[i]);
58+
               result.add(list);
59+
          }
60+
      }
61+
       return new ArrayList<>(result);
62+
  }
63+
64+
   private List<List<Integer>> combinationSum2(int[] candidates, int index, int target) {
65+
       List<List<Integer>> result = new ArrayList<>();
66+
       if (target < candidates[index]) {
67+
           return result;
68+
      }
69+
       if (Arrays.binarySearch(candidates, index + 1, candidates.length, target) > 0) {
70+
           result.add(new ArrayList<>(Collections.singletonList(target)));
71+
      }
72+
       for (int i = index + 1; i < candidates.length; i++) {
73+
           for (List<Integer> list : this.combinationSum2(candidates, i, target - candidates[i])) {
74+
               list.add(candidates[i]);
75+
               result.add(list);
76+
          }
77+
      }
78+
       return result;
79+
  }
80+
}
81+
           for (List<Integer> list : this.combinationSum2(candidates, i, target - candidates[i])) {
82+
```
83+
![](https://ws3.sinaimg.cn/large/006tKfTcgy1g1bpbuuppkj311a0q6tcn.jpg)

0 commit comments

Comments
 (0)