40. 组合总和 II
题目描述:
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
题解:
class Solution {
//这是回溯法
public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
getList(candidates,0,target,new Stack<>(),result);
return result;
}
public static void getList(int[] candisates, int index, int target, Stack<Integer> stack, List<List<Integer>> comb){
//判断遍历终止的代码
if(target == 0){
comb.add(new ArrayList<>(stack));
return;
}
//进行遍历
for(int in = index;in < candisates.length && (target >= candisates[in]);in ++){
//如果该数据与上一个数据相等就跳过--针对重复现象的解决办法,注意里面用到index,可以防止同样父节点下出现相同的子节点
if(in > index && candisates[in] == candisates[in-1]){
continue;
}
stack.push(candisates[in]);
getList(candisates,in+1,target - candisates[in],stack,comb);
stack.pop();
}
}
}
40. 组合总和 II的更多相关文章
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- 40. 组合总和 II leetcode JAVA
题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...
- LeetCode 40. 组合总和 II(Combination Sum II)
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 组合总和 II
组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
随机推荐
- hdu6325 /// 上凸包
题目大意: 给定n 为n个点 给定n个点的坐标 两个点(xi,yi) (xj,yj)之间的花费是 xi*yj-yi*xj (可能为负数) 要求从点1经过若干个点到点n最小花费的路径 且路径要按x轴方向 ...
- C#异步编程----async和await组合的写法
微软示例: private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync ...
- 在VSCode中使用Markdown
前言 最近在学习使用Markdown语法,尝试使用了"MarkdownEditor"."Sublime Text3"."VSCode"这三种 ...
- vue 中使用 lazyload 插件 数据更图片不更新 的原因 及解决方案
在使用lazyload插件的img标签上,加上:key标识即可
- 小程序怎样将字符串转化为html
在小程序中通过rich-text 标签,使用nodes将字符串转化为html <rich-text class="allAnswer " nodes='<span c ...
- 视频质量评测标准——VMAF
阿里云视频云直播转码每天都会处理大量的不同场景.不同编码格式的直播流.为了保证高画质,团队借助VMAF标准来对每路转码的效果做质量评估,然后进行反馈.调优.迭代.这么做的原因在于,像动作片.纪录片.动 ...
- NX二次开发-UFUN遍历函数UF_OBJ_cycle_objs_in_part
NX11+VS2013 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include < ...
- NX二次开发-UFUN结合NXOPEN开发_常用代码模板
hpp //头文件 #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <N ...
- NX二次开发-C++ CopyFile函数的用法
NX9+VS2012 #include<Windows.h> CopyFile("D:\\test.prt","D:\\1\\test123.prt" ...
- vue中利用scss实现整体换肤和字体大小设置
一.前言 利用Sass预处理实现换肤和字体大小调整. 思路及达到的效果:字体大小的适配使用window.devicePixelRatio的值和需要调整的差量进行控制.页面初始化是的字体适配可以根据de ...