当前位置:网站首页>Combination sum II of leetcode topic analysis
Combination sum II of leetcode topic analysis
2022-06-23 08:59:00 【ruochen】
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations. For example, given candidate set 10,1,2,7,6,1,5 and target 8,
A solution set is:
1, 7
1, 2, 5
2, 6
1, 1, 6
- Combination Sum Each element can be used more than once , So recursion is dfs(i, target - candidatesi, result, cur, candidates);
- Combination Sum II Each element can only be used once , So recursion is dfs(i + 1, target - candidatesi, rt, cur, candidates); Because the solution may be repeated , So use set, Finally, it turns into list.
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if (candidates == null || candidates.length == 0) {
return new ArrayList<List<Integer>>();
}
Set<List<Integer>> rt = new HashSet<List<Integer>>();
ArrayList<Integer> cur = new ArrayList<Integer>();
Arrays.sort(candidates);
dfs(0, target, rt, cur, candidates);
return new ArrayList<List<Integer>>(rt);
}
private void dfs(int start, int target, Set<List<Integer>> rt,
ArrayList<Integer> cur, int[] candidates) {
if (target == 0) {
rt.add(new ArrayList<Integer>(cur));
return;
}
for (int i = start; i < candidates.length; i++) {
// candidates[i] > target, Then the recursion ends , There can be no solution
if (candidates[i] > target) {
return;
}
cur.add(candidates[i]);
dfs(i + 1, target - candidates[i], rt, cur, candidates);
cur.remove(cur.size() - 1);
}
}边栏推荐
- Single core driver module
- Use newbeecoder UI implements data paging
- 6月《中国数据库行业分析报告》发布!智能风起,列存更生
- JS mask important data of ID card and mobile phone number with * *
- New engine, new capability, new experience, Tencent host security flagship release
- 'coach, I want to play basketball!'—— AI Learning Series booklet for system students
- C # advanced learning -- virtual method
- How to use the template library of barcode label software
- Mqtt+flink to subscribe and publish real-time messages
- 986. Interval List Intersections
猜你喜欢

Linux MySQL installation

3. Caller 服务调用 - dapr

Monitor the cache update of Eureka client

简易学生管理

297. Serialize and Deserialize Binary Tree

Testing -- automated testing selenium (about API)
![Paper reading [quovadis, action recognition? A new model and the dynamics dataset]](/img/3f/449cc91bfa66fcf26bc2cd405fb773.png)
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]

In June, China database industry analysis report was released! Smart wind, train storage and regeneration

986. Interval List Intersections

瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
随机推荐
Unique paths for leetcode topic resolution
[QNX Hypervisor 2.2用户手册]6.2 网络
Custom tag - JSP tag Foundation
2022.6.22-----leetcode.513
6月《中国数据库行业分析报告》发布!智能风起,列存更生
Leetcode topic analysis count primes
When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
Leetcode topic analysis 3sum closest
Utilisation du cookie du module de demande de noeud
Leetcode topic analysis group anagrams
297. Serialize and Deserialize Binary Tree
Leetcode topic analysis spiral matrix II
【云原生 | Kubernetes篇】Kubernetes原理与安装(二)
How to use "tomato working method" in flowus, notation and other note taking software?
The results of CDN node and source station are inconsistent
Subsets of leetcode topic resolution
(resolved) difference between leftmost prefix and overlay index
Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
Interpretation of the most dirty technology in history, I can understand 60 it terms in seconds
6、 Web Architecture Design