题目:

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用一次。

说明:

  • 所有数字(包括目标数)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

  1. 输入: candidates = [10,1,2,7,6,1,5], target = 8,
  2. 所求解集为:
  3. [
  4. [1, 7],
  5. [1, 2, 5],
  6. [2, 6],
  7. [1, 1, 6]
  8. ]

示例 2:

  1. 输入: candidates = [2,5,2,1,2], target = 5,
  2. 所求解集为:
  3. [
  4.   [1,2,2],
  5.   [5]
  6. ]

解题思路:

首先将数组排序,然后递归地找到符合target的数组组合,最后除去重复的数组。

  1. class Solution {
  2. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. Arrays.sort(candidates);
  5. getAnswers(res,candidates,target,new ArrayList<>(),0);
  6. return res;
  7. }
  8.  
  9. public void getAnswers(List<List<Integer>> res, int[] candidates, int target,
  10. List<Integer> tempList,int index) {
  11. if (target == 0) {
  12. if(!res.contains(tempList))
  13. res.add(tempList);
  14. return;
  15. }
  16. for (int i = index; i < candidates.length; i++) {
  17. if (candidates[i]<=target) {
  18. List<Integer> list=new ArrayList<>(tempList);
  19. list.add(candidates[i]);
  20. getAnswers(res,candidates,target-candidates[i],list,i + 1);
  21. } else {
  22. break;
  23. }
  24. }
  25. }
  26. }
  1.  

40. 组合总和 II leetcode JAVA的更多相关文章

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

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

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

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 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. LeetCode 40. 组合总和 II(Combination Sum II)

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

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

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

  8. 40. 组合总和 II

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

  9. 组合总和 II

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

随机推荐

  1. DAVINCI开发原理

    本文中约定: [host] 表示主机PC机Linux [target] 表示目标板Linux DAVINCI开发原理之一----ARM端开发环境的建立(DVEVM) 1. 对DAVINCI平台,TI在 ...

  2. python要点之III

    [python要点之III] 1.实现交换. 在C/C++中,交换两个变量,需要2个变量,tmp=x;x=y;y=tmp;. 在python中,交换两个变量可以这么写:x,y=y,x. 2.is&am ...

  3. 高性能Web服务器Nginx的配置与部署研究(9)核心模块之HTTP模块基本常用指令

    一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.指令 1. alias 含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适 ...

  4. css的优先级和权重问题 以及!important优先级

    一,前言: 刚加的css怎么没有渲染出来?浏览器中查看,发现是被其他的css给覆盖了,相信我们都曾遇到过这样的问题.那么浏览器是如何选择css标签的渲染顺序的呢?换句话说,css选择器的优先级是怎么规 ...

  5. sqlserver计算日期

    在网上找到的一篇文章,相当不错哦O(∩_∩)O~ 这是计算一个月第一天的SQL 脚本:  SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) --当月的第一 ...

  6. Lucas–Kanade光流算法学习

    Lucas–Kanade光流算法是一种两帧差分的光流估计算法.它由Bruce D. Lucas 和 Takeo Kanade提出.         光流(Optical flow or optic f ...

  7. 阿里云WindowsServer2012安装IIS失败

    本文地址:http://www.cnblogs.com/drfxiaoliuzi/p/6388417.html 首先,向微软官方论坛的大神致敬: https://social.technet.micr ...

  8. 设置ctp文件按html文件解析

  9. Linux 设置默认编辑器(以nano为例)

    查看nano地址 which nano output: /usr/bin/nano 设置默认编辑器 nano ~/.bashrc export EDITOR=nano alias vi=/usr/bi ...

  10. Spring框架总结(五)

    自动装配(了解) 根据名称自动装配:autowire="byName" 自动去IOC容器中找与属性名同名的引用的对象,并自动注入 延续使用user.dao.service.acti ...