当前位置:网站首页>Combination sum of leetcode topic analysis
Combination sum of leetcode topic analysis
2022-06-23 08:59:00 【ruochen】
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
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 2,3,6,7 and target 7,
A solution set is:
7
2, 2, 3
Using recursion , Note when saving results , We should build a new one ArrayList:result.add(new ArrayList< Integer>(cur)). otherwise result Will point to the ever-changing cur.
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if (candidates == null || candidates.length == 0) {
return new ArrayList<List<Integer>>();
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
ArrayList<Integer> cur = new ArrayList<Integer>();
Arrays.sort(candidates);
dfs(0, target, result, cur, candidates);
return result;
}
private void dfs(int start, int target, List<List<Integer>> result,
ArrayList<Integer> cur, int[] candidates) {
if (target == 0) {
result.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, target - candidates[i], result, cur, candidates);
cur.remove(cur.size() - 1);
}
}边栏推荐
- Quartz Crystal Drive Level Calculation
- 在小程序中实现视频通话及互动直播的一种方法
- The most commonly used 5-stream ETL mode
- Arthas vmtool命令小结
- 1-gradients, shadows, and text
- Part of speech family
- 2022.6.22-----leetcode.513
- Only 187 bytes of desktop dream code
- On the light application platform finclip and the mobile application development platform mpaas
- [qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
猜你喜欢

自定义标签——jsp标签基础

636. Exclusive Time of Functions

自定义标签——jsp标签增强

点击添加下拉框
![[learning resources] understand and love mathematics](/img/a3/e1b0915c48c85d17c48a4bee523424.png)
[learning resources] understand and love mathematics

Object. Defineproperty() and data broker

6月《中國數據庫行業分析報告》發布!智能風起,列存更生

"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems

Quartz Crystal Drive Level Calculation

173. Binary Search Tree Iterator
随机推荐
如何在 FlowUs、Notion 等笔记软件中使用「番茄工作法」?
Arclayoutview: implementation of an arc layout
How to restore visualizations and dashboards after kibana rebuilds the index
How postman does interface testing 1: how to import swagger interface documents
438. Find All Anagrams in a String
Leetcode topic analysis 3sum closest
瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
Summary of Arthas vmtool command
636. Exclusive Time of Functions
Hongmeng reads the resource file
@Response
Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis
Talk about the implementation principle of @autowired
Leetcode topic analysis sort colors
Community article | mosn building subset optimization ideas sharing
Unity grid programming 06
C # advanced learning -- virtual method
528. Random Pick with Weight
JSP入门总结
Top 25 most popular articles on vivo Internet technology in 2021