当前位置:网站首页>Leetcode T39: 组合总和
Leetcode T39: 组合总和
2022-07-01 08:08:00 【范谦之】
题目描述
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []
思路
很常规的dfs题,但是由于很久没打dfs代码了,记录一下。
代码
List<List<Integer>> res = new ArrayList<List<Integer>>();
int n, tar;
int[] nums;
// 当前选取的组合,选到了第几个数字,当前和
void dfs(List<Integer> lis, int cur, int s) {
if(cur == n) {
if(s == tar)
res.add(lis);
} else {
// for(int x: lis) System.out.print(x+"\t"); System.out.println("\t,"+cur+"-"+s);
//对于当前数字可选,可不选
// 选,看是否超过,剪枝
if(s + nums[cur] <= tar) {
List<Integer> tmp = new ArrayList<Integer>(lis);
tmp.add(nums[cur]);
dfs(tmp, cur, s + nums[cur]);
}
// 不选
if(true) {
List<Integer> tmp = new ArrayList<Integer>(lis);
dfs(tmp, cur+1, s);
}
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
nums = candidates;
n = candidates.length;
tar = target;
dfs(new ArrayList<Integer>(), 0, 0);
return res;
}
边栏推荐
- 力扣每日一题-第31天-1502.判断能否形成等差数列
- Rk3399 platform development series explanation (network debugging) 7.30. What will affect the sending process of TCP packets?
- String coordinates of number to excel
- Li Kou daily question - day 31 -202 Happy number
- 5大组合拳,解决校园6大难题,护航教育信息化建设
- Airsim radar camera fusion to generate color point cloud
- How to prevent the other party from saying that he has no money after winning the lawsuit?
- Serial port oscilloscope software ns-scope
- getInputStream() has already been called for this request
- [getting started] input n integers and output the smallest K of them
猜你喜欢

【入门】截取字符串

How outlook puts together messages with the same discussion

軟鍵盤高度報錯

On several key issues of digital transformation

软键盘高度报错

Koltin35, headline Android interview algorithm

Adding color blocks to Seaborn clustermap matrix

Instead of houses, another kind of capital in China is rising
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

P4 installation bmv2 detailed tutorial
随机推荐
Adding color blocks to Seaborn clustermap matrix
Rumtime 1200 upgrade: London upgrade support, pledge function update and more
Aardio - Shadow Gradient Text
AArdio - 【问题】bass库回调时内存增长的问题
go通用动态重试机制解决方案的实现与封装
web254
[untitled]
【入门】截取字符串
[force deduction 10 days SQL introduction] Day10 control flow
Serial port oscilloscope software ns-scope
Utiliser Beef pour détourner le navigateur utilisateur
Access报表实现小计功能
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
getInputStream() has already been called for this request
2022.6.30 省赛+蓝桥国赛记录
Two expressions of string
How can beginners correctly understand Google's official suggested architectural principles (questions?)
LM08丨网格系列之网格反转(精)
Differential: definition of total differential, partial derivative, gradient
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion