1. 原题链接

https://leetcode.com/problems/combination-sum-ii/description/

2. 题目要求

给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合

注意:(1)每个可能的答案中,数组中的每一个元素只能使用一次;(2)数组存在重复元素;(3)数组中都是正整数;(4)不能存在重复解

3. 解题思路

这与第39题 Combination Sum 看起来很是类似,但一些细节要求完全不同,因此处理起来的方法也不相同。相同的是依旧采用递归的方法。

不存在重复解,但给定的数组中存在重复元素,因此先对candidates[ ]进行排序,方便处理重复元素的问题

4. 代码实现

 import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSum40 {
public static void main(String[] args) {
CombinationSum40 cs = new CombinationSum40();
int[] candidates = {10, 1, 2, 7, 6, 1, 5};
for (List l : cs.combinationSum2(candidates, 8))
System.out.println(l);
} public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
for(int x:candidates)
System.out.print(x+" ");
System.out.println();
List<List<Integer>> result = new ArrayList<List<Integer>>();
combinationSum(result, new ArrayList<Integer>(), candidates, 0, target);
return result; } public void combinationSum(List<List<Integer>> result, List<Integer> tmp, int[] candidates, int start, int target) {
if (target > 0) {
for (int i = start; i < candidates.length; i++) {
if (i > start && candidates[i] == candidates[i - 1]) // 避免重复的结果
continue; // 结束for循环中其后的语句,跳回for循环
tmp.add(candidates[i]);
combinationSum(result, tmp, candidates, i + 1, target - candidates[i]);
tmp.remove(tmp.size() - 1); // 累加和超过target,则删去列表表尾元素
}
}
if (target == 0) {
result.add(new ArrayList<>(tmp));
} }
}

LeetCode:40. Combination Sum II(Medium)的更多相关文章

  1. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  2. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  3. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  4. LeetCode OJ 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  6. LeetCode:12. Integer to Roman(Medium)

    1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...

  7. LeetCode 40. Combination Sum II (组合的和之二)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

  9. [LeetCode] Combination Sum II (递归)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. 转 delete 和 delete []的真正区别

    c++中对new申请的内存的释放方式有delete和delete[两种方式,到底这两者有什么区别呢? 1.我们通常从教科书上看到这样的说明:delete 释放new分配的单个对象指针指向的内存dele ...

  2. UVa 753 - A Plug for UNIX(最大流)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. 虚拟机Centos安装docker小记

    本书记录是参考 <Spring Cloud 与 Docker 微服务架构实战(第二版)>这本书实现的. 记录简单几个命令,安装顺序如下: 1. 安装docker所需的包 sudo yum ...

  4. 【luogu P1351 联合权值】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1351 做了些提高组的题,不得不说虽然NOIP考察的知识点虽然基本上都学过,但是做起题来还是需要动脑子的. 题 ...

  5. sql传统的拼接带来的危害

    现在大家来学习下sql是如何注入的,传统的拼接字符串会造成 注入形式就是在变量那使用<1=1>这样查询无论怎样都是正确的 -- var sql = "select name fr ...

  6. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  7. springboot集成activiti工作流时容易出现的问题

    No.1 启动报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'or ...

  8. php auto_prepend_file和auto_append_file的妙用

    这是一个鲜为人知的设置! auto_prepend_file 和 auto_append_file 是在php.ini中进行配置的2个参数,auto_prepend_file 表示在php程序加载第一 ...

  9. linux安装完jenkins无法访问的问题

    jenkins是一个持续集成部署的工具,非常好用,当然,有些细节问题还是需要注意的.当我们兴高采烈的安装完jenkins的时候,然后发现并不能访问,真是气死了. 这个时候就要一步一步检测问题出在哪. ...

  10. 爬虫——Scrapy框架案例二:阳光问政平台

    阳光热线问政平台 URL地址:http://wz.sun0769.com/index.php/question/questionType?type=4&page= 爬取字段:帖子的编号.投诉类 ...