题目描述:

给定一个数组 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的更多相关文章

  1. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  2. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  3. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  4. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  5. Leetcode题库——40.组合总和II

    @author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...

  6. 40. 组合总和 II leetcode JAVA

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...

  7. LeetCode 40. 组合总和 II(Combination Sum II)

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...

  8. leetcode 40. 组合总和 II (python)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  9. 组合总和 II

    组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

随机推荐

  1. 十、 shell基础

    shell的表现形式: history -c 清空历史命令(清空缓存;默认:1000条) -w 将缓存中的历史命令保存到配置文件中 ~/.bash_history 永久保存历史命令(默认:1000条) ...

  2. hdu6537 /// DP 最长不降子序列->最长公共子序列

    题目大意: 给定一个字符串 字符为0~9 求翻转某个区间后使得串中的最长不降子序列最长 因为字符范围为0~9 假设有一个 0 1 2 3 4 5 6 7 8 9 的序列 此时翻转某个区间得到形如 0 ...

  3. springboot启动方式

    第一种(本地开发使用)在项目的根目录下有一个带有main函数的Application类,可以直接执行这个main函数.(使用的是SpringBoot内置的tomcat) 第二种(本地开发使用)在pom ...

  4. day02 mysql基本语句 存储引擎 数据类型 完整性约束

    day02 mysql   一.sql的基本语句     系统数据库         information_schema: 虚拟库, 不占磁盘空间(在内存汇中): 存的是数据库启动后的一些参数, 如 ...

  5. python 打印出水仙花数

    打印出三位水仙花数方法及解释 num = 100while num <= 999: #这里num 小于等于999 则运行 填1000也可以 a = num % 10 #num对10取余 b = ...

  6. 解析Mybatis入门第一天

    MyBatis是一个基于Java的持久层框架,内部对JDBC做了封装,使开发者只需要关注SQL语句,而不用关注JDBC的代码,使开发变得更加的简单. MyBatis通过XML或者注解的方式将要执行的各 ...

  7. NX二次开发-UFUN获取直线的两个端点UF_CURVE_ask_line_data

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_ui.h> UF_initialize(); ...

  8. C 函数指针详解

    一 通常的函数调用    一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int a ...

  9. 1.RabbitMQ介绍

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序之间 ...

  10. 洛谷P2184——贪婪大陆

    传送门:QAQQAQ 题意:给一个长度为$n$的区间,每次可以进行两种操作: 1.在$[l,r]$这个区间里放置一个和之前种类不同的炸弹 2.查询在$[l,r]$区间内有多少种不同种类的炸弹 思路:第 ...