[leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
直接暴力思路,用dfs+回溯枚举所有可能组合情况。难点在于每个数可取无数次。
我的枚举思路是:
外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(candidates)终止。
然后内层就可以dfs搜索了,
缕清状态的转换与回溯,题就做出来了。
dfs的状态函数:dfs(int k, int i, int[] candidates, List now, int nowCnt, int target, List<List> ans)
k 代表当前状态下还能拿k个数
i 代表已经搜到了数组candidates中的第i个数
now 代表当前已经拿的数的list
nowCnt 代表当前状态下的和
由此可见,当k0且nowCnttarget时,表示已经搜到一个解了,此时把解add到ans即可
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
if (candidates.length == 0) return new ArrayList<>();
Arrays.sort(candidates);
int maxCnt = target / candidates[0] + 1;
for (int k = 1; k < maxCnt; k++) {
dfs(k, 0, candidates, new ArrayList<>(), 0, target, ans);
}
return ans;
}
public void dfs(int k, int i, int[] candidates, List<Integer> now, int nowCnt, int target, List<List<Integer>> ans) {
if (nowCnt > target) {
return;
}
if (k == 0 && nowCnt == target) {
ans.add(new ArrayList<>(now));
return;
}
if (i >= candidates.length) return;
int num = candidates[i];
int max = target / num;
max = Math.min(max, k);
for (int j = 0; j <= max; j++) {
if (nowCnt + j * num > target) {
break;
}
for (int q = 1; q <= j; q++) {
now.add(num);
}
dfs(k - j, i + 1, candidates, now, nowCnt + j * num, target, ans);
for (int q = now.size() - 1; q >= 0; q--) {
if (now.get(q) == num) {
now.remove(q);
}
}
}
}
}
[leetcode] 39. 组合总和(Java)(dfs、递归、回溯)的更多相关文章
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
- [leetcode] 37. 解数独(Java)(dfs,递归,回溯)
37. 解数独 1A 这个题其实15分钟左右就敲出来并且对了...但是由于我输错了一个数..导致我白白debug一个多小时.. 没啥难度,练递归-dfs的好题 class Solution { pri ...
- Java实现 LeetCode 39 组合总和
39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
- leetcode 39. 组合总和(python)
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- LeetCode——39. 组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- LeetCode 39. 组合总和(Combination Sum)
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限 ...
- 【LeetCode】39. 组合总和
39. 组合总和 知识点:递归:回溯:组合:剪枝 题目描述 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 ...
- Java实现 LeetCode 216. 组合总和 III(三)
216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...
随机推荐
- js的13种继承
js继承的13种方式 也可以说只有12种,ES6的extend 也是12种方法之一-寄生继承的语法糖 1.原型链法 代码示例 Child.prototype = new Parent(); 所属模式: ...
- SAAS云平台搭建札记: (四) AntD For React使用react-router-dom路由接收不同参数页面不刷新的问题
在.net开发员眼里,如果使用MVC,根据路由匹配原则,可以通过各种方式接收参数,比如 /Post/List/1, /Post/List/2,或者 /Post/List?id=1,/Post/List ...
- k8s kubernetes给node节点添加标签和删除node节点标签
node节点IP 192.168.1.205 给节点添加标签的命令 添加label语法 kubectl label nodes <node-name> <label-key>= ...
- Sql server注入一些tips
sql server环境测试: 几个特性: 1.sql server兼容性可以说是最差的. 举例: select x from y where id=1 字符串查询 select x from y w ...
- 1016 Phone Bills
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- Sublime插件安装和使用
Sublime插件安装和使用 插件安装的方式: 插件安装方式一:直接安装 下载插件安装包,然后把安装解压到packages目中,按成安装(菜单->首选项->浏览插件) 插件安装方法二:使用 ...
- 病毒木马查杀实战第016篇:U盘病毒之逆向分析
比对脱壳前后的程序 我们这次所要研究的是经过上次的脱壳操作之后,所获取的无壳病毒样本.其实我们这里可以先进行一下对比,看看有壳与无壳的反汇编代码的区别.首先用IDA Pro载入原始病毒样本: 图1 可 ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- Windows PE 第四章 导入表
第四章 导入表 导入表是PE数据组织中的一个很重要的组成部分,它是为实现代码重用而设置的.通过分析导入表数据,可以获得诸如OE文件的指令中调用了多少外来函数,以及这些外来函数都存在于哪些动态链接库里等 ...
- JavaScript 中正则匹配时结果不一致的问题
创建示例项目 考察如下场景,我们有个输入框组件,输入时同时进行校验. interface IInputProps { label: string; } function Input({ label } ...