当前位置:网站首页>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);
}
}边栏推荐
- When easynvr service is started, video cannot be played due to anti-virus software interception. How to deal with it?
- TDesign update weekly report (the first week of January 2022)
- In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
- Talk about the implementation principle of @autowired
- Intelligent operation and maintenance exploration | anomaly detection method in cloud system
- 1、 Software architecture evaluation
- Balls and cows of leetcode topic analysis
- Testing -- automated testing selenium (about API)
- '教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
- GeoServer adding mongodb data source
猜你喜欢

986. Interval List Intersections

力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)

Monitor the cache update of Eureka client

6月《中国数据库行业分析报告》发布!智能风起,列存更生

986. Interval List Intersections

【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启

Quartz Crystal Drive Level Calculation

Community article | mosn building subset optimization ideas sharing

Testing -- automated testing selenium (about API)

GeoServer adding mongodb data source
随机推荐
Summary of communication mode and detailed explanation of I2C drive
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
Fraction to recursing decimal
Custom tag - JSP tag Foundation
js 用**遮罩身份证以及手机号的重要数据
【云原生 | Kubernetes篇】Kubernetes原理与安装(二)
Leetcode topic analysis sort colors
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
[learning resources] understand and love mathematics
MySQL故障案例 | ERROR 1071 (42000): Specified key was too long
How thingjs enables low threshold 3D visualization development
GeoServer adding mongodb data source
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
MySQL fault case | error 1071 (42000): specified key was too long
6月《中国数据库行业分析报告》发布!智能风起,列存更生
Lighthouse cloud desktop experience
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
How to use "tomato working method" in flowus, notation and other note taking software?
3. caller service call - dapr