当前位置:网站首页>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);
}
}边栏推荐
- Hongmeng reads the resource file
- 如何在 FlowUs、Notion 等笔记软件中使用「番茄工作法」?
- 简易学生管理
- Monitor the cache update of Eureka client
- Community article | mosn building subset optimization ideas sharing
- On the light application platform finclip and the mobile application development platform mpaas
- USB peripheral driver - configfs
- H-index of leetcode topic analysis
- 类型从属名称的使用必须以“typename”为前缀
- [operating steps] how to set the easynvr hardware device to be powered on without automatic startup?
猜你喜欢

测试-- 自动化测试selenium(关于API)

297. Serialize and Deserialize Binary Tree

Qualcomm 9x07 two startup modes

Custom tag - JSP tag Foundation

The fourth online workshop review

GeoServer adding mongodb data source

Simple student management

986. Interval List Intersections

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

Geoserver添加mongoDB数据源
随机推荐
Leetcode topic analysis group anagrams
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“
Deep analysis and Simulation of vector
[cloud native | kubernetes] kubernetes principle and installation (II)
Single core driver module
Geoserver添加mongoDB数据源
Unique paths for leetcode topic resolution
Leetcode topic analysis set matrix zeroes
Flutter achieves the effect of selecting seats in the cinema!
986. Interval List Intersections
636. Exclusive Time of Functions
New engine, new capability, new experience, Tencent host security flagship release
How postman does interface testing 1: how to import swagger interface documents
[learning resources] understand and love mathematics
1、 Software architecture evaluation
扫码登录基本流程
如何在 FlowUs、Notion 等笔记软件中使用「番茄工作法」?
65. Valid Number
173. Binary Search Tree Iterator