Skip to content

Commit c11c82a

Browse files
committed
39. Combination Sum
1 parent 98005ee commit c11c82a

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/leetcode/_39_/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._39_;
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.combinationSum(new int[]{48, 22, 49, 24, 26, 47, 33, 40, 37,
11+
39, 31, 46, 36, 43, 45, 34, 28, 20, 29, 25, 41, 32, 23}, 69));
12+
}
13+
}
14+

src/leetcode/_39_/Solution.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode._39_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Set;
8+
import java.util.TreeSet;
9+
import java.util.stream.Collectors;
10+
11+
class Solution {
12+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
13+
Set<Integer> candidateSet = Arrays.stream(candidates).boxed().collect(Collectors.toCollection(TreeSet::new));
14+
return this.combinationSum(candidateSet, target);
15+
}
16+
17+
// here def a new func to avoid calculate a new Set every time
18+
private List<List<Integer>> combinationSum(Set<Integer> candidateSet, int target) {
19+
List<List<Integer>> result = new ArrayList<>();
20+
if (candidateSet.contains(target)) {
21+
result.add(new ArrayList<>(Collections.singleton(target)));
22+
}
23+
for (Integer candidate : candidateSet) {
24+
int newTarget = target - candidate;
25+
if (newTarget < candidate) {
26+
return result;
27+
} else {
28+
for (List<Integer> list : this.combinationSum(candidateSet, newTarget)) {
29+
// this ensure all result are desc sorted to avoid repetition
30+
if (candidate <= list.get(list.size() - 1)) {
31+
list.add(candidate);
32+
result.add(list);
33+
}
34+
}
35+
}
36+
}
37+
return result;
38+
}
39+
}

src/leetcode/_39_/solution.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
### [39\. Combination SumCopy for Markdown](https://leetcode.com/problems/combination-sum/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a **set** of candidate numbers (`candidates`) **(without duplicates)** and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sums to `target`.
7+
8+
The **same** repeated number may be chosen from `candidates` unlimited number of times.
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 = [2,3,6,7], target = 7,
19+
A solution set is:
20+
[
21+
[7],
22+
[2,2,3]
23+
]
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: candidates = [2,3,5], target = 8,
30+
A solution set is:
31+
[
32+
  [2,2,2,2],
33+
  [2,3,3],
34+
  [3,5]
35+
]
36+
```
37+
38+
39+
#### Solution
40+
41+
Language: **Java**
42+
43+
```java
44+
class Solution {
45+
   public List<List<Integer>> combinationSum(int[] candidates, int target) {
46+
       Set<Integer> candidateSet = Arrays.stream(candidates).boxed().collect(Collectors.toCollection(TreeSet::new));
47+
       return this.combinationSum(candidateSet, target);
48+
  }
49+
50+
   // here def a new func to avoid calculate a new Set every time
51+
   private List<List<Integer>> combinationSum(Set<Integer> candidateSet, int target) {
52+
       List<List<Integer>> result = new ArrayList<>();
53+
       if (candidateSet.contains(target)) {
54+
           result.add(new ArrayList<>(Collections.singleton(target)));
55+
      }
56+
       for (Integer candidate : candidateSet) {
57+
           int newTarget = target - candidate;
58+
           if (newTarget < candidate) {
59+
               return result;
60+
          } else {
61+
               for (List<Integer> list : this.combinationSum(candidateSet, newTarget)) {
62+
                   // this ensure all result are desc sorted to avoid repetition
63+
                   if (candidate <= list.get(list.size() - 1)) {
64+
                       list.add(candidate);
65+
                       result.add(list);
66+
                  }
67+
              }
68+
          }
69+
      }
70+
       return result;
71+
  }
72+
}
73+
```
74+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g1bmizhcvoj310j0u0dkq.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy