当前位置:网站首页>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);
}
}边栏推荐
- 1、 Software architecture evaluation
- 多线程初学
- Happy number of leetcode topic analysis
- (resolved) difference between leftmost prefix and overlay index
- Summary of Arthas vmtool command
- In June, China database industry analysis report was released! Smart wind, train storage and regeneration
- 【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
- Leetcode topic analysis set matrix zeroes
- In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
- Testing -- automated testing selenium (about API)
猜你喜欢

173. Binary Search Tree Iterator

Summary of communication mode and detailed explanation of I2C drive

点击添加下拉框

In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain

瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目

The most commonly used 5-stream ETL mode

Testing -- automated testing selenium (about API)

Quartz Crystal Drive Level Calculation

Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis
![[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24](/img/e1/97c92290a2a5e68f05cdbd5bf525e8.png)
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
随机推荐
Summary of communication mode and detailed explanation of I2C drive
js 用**遮罩身份证以及手机号的重要数据
Spirit matrix for leetcode topic analysis
Subsets of leetcode topic resolution
Driver Architecture & platform platform bus driver model
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
Leetcode topic analysis h-index II
Quartz Crystal Drive Level Calculation
[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
Linux MySQL installation
Kernel log debugging method
[qnx hypervisor 2.2 user manual]6.2 network
Unique paths II of leetcode topic analysis
Leetcode topic analysis sort colors
node request模块cookie使用
5、 Project management
USB peripheral driver - configfs
1、 Software architecture evaluation
Monitor the cache update of Eureka client