Description:

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

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8
A solution set is:

[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
] Thoughts:
这个问题和前一个问题Combination Sum很像,区别在于它允许给定的nums中有重复数字,但是不允许对nums中的数字进行重复使用.同时还不能够出现重复的解
解决不允许对nums中的数字进行重复使用,我们只需要将下一次回溯的开始点设置为当前回溯点的下一个点即可。不能出现重复解,这个问题我们可以通过在添加解的时候判断当前的解是否已经在已有的解中来解决(另外一种解决方式是
通过添加限制条件来减少当前解的搜索空间。因为我们不允许出现重复解,但是nums中又有重复数字,因此我们只需要保证搜索路径跳过重复的数字即可。) 以下是java解法一:
package middle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSum { public List<List<Integer>> combinationSum(int[] candidates, int target){
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
backtrack(list, new ArrayList(),candidates, target, 0);
/* System.out.println(list);*/
return list;
} private void backtrack(List<List<Integer>> list, ArrayList arrayList,
int[] candidates, int target, int start) {
// TODO Auto-generated method stub
if(target < 0){
return;
}else if(target == 0){
//we should new an ArrayList, because the arrayList will change later,
//if we do not copy it's value, list will add []
list.add(new ArrayList<Integer>(arrayList));
}else{
for(int i = start;i<candidates.length;i++){
arrayList.add(candidates[i]);
backtrack(list, arrayList, candidates, target-candidates[i], i);
arrayList.remove(arrayList.size() - 1);
}
}
} public static void main(String[] args){
CombinationSum sum = new CombinationSum();
int[] candidates = new int[]{2, 3,5 ,7};
sum.combinationSum(candidates, 7);
}
}

  这个解法采用的是通过在添加解的时候判断当前的解是否已经在已有的解中来解决重复问题

  以下是java解法二:

package middle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class CombinationSumTwo { public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
backTrack2(list, new ArrayList(), candidates, target, 0);
return list;
} private void backTrack(List<List<Integer>> list, ArrayList arrayList,
int[] candidates, int target, int start) {
// TODO Auto-generated method stub if(target < 0){
return;
}else if (target == 0){
if(list.contains(arrayList) == false){
list.add(new ArrayList(arrayList));
}
}else{
for(int i = start; i < candidates.length;i++){
arrayList.add(candidates[i]);
backTrack(list, arrayList, candidates, target-candidates[i], i+1);
arrayList.remove(arrayList.size() - 1);
}
}
} private void backTrack2(List<List<Integer>> list, ArrayList arrayList, int[] candidates, int target, int start){
if(target < 0){
return;
}else if(target == 0){
//we should new an ArrayList, because the arrayList will change later,
//if we do not copy it's value, list will add []
list.add(new ArrayList<Integer>(arrayList));
}else{
for(int i = start;i<candidates.length;i++){
if(i > start && candidates[i] == candidates[i - 1]) continue;
arrayList.add(candidates[i]);
backTrack2(list, arrayList, candidates, target-candidates[i], i+1);
arrayList.remove(arrayList.size() - 1);
}
}
} public static void main(String[] args){
CombinationSumTwo c = new CombinationSumTwo();
int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
List<List<Integer>> l = c.combinationSum2(candidates, 8);
System.out.println(l);
} }

这个解法采用了减少搜索空间的方式来去除重复的解。

Combination Sum Two的更多相关文章

  1. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum II 组合之和之二

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

  4. [LeetCode] Combination Sum 组合之和

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

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  8. 【leetcode】Combination Sum II

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

  9. 【leetcode】Combination Sum

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  10. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

随机推荐

  1. 海量数据挖掘MMDS week6: 决策树Decision Trees

    http://blog.csdn.net/pipisorry/article/details/49445465 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  2. C语言assert的用法

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#include <assert.h>void assert( int ...

  3. listview优化(中)

    1,对Imageview使用setTag()方法来解决图片错位问题,这个Tag中设置的是图片的url,然后在加载的时候取得这个url和要加载那position中的url对比,如果不相同就加载,相同就是 ...

  4. 文件I/O实践(1) --基础API

    什么是I/O 输入/输出是内存和外设之间拷贝数据的过程: 设备->内存: 输入操作 内存->设备: 输出操作 高级I/O: ANSI C提供的标准I/O库函数成为高级I/O, 也称为带缓冲 ...

  5. Css详解之(选择器)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Linux Platform Device and Driver

    从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...

  7. STL - 容器共性机制研究

    C++模板是容器的概念. 理论提高:所有容器提供的都是值(value)语意,而非引用(reference)语意.容器执行插入元素的操作时,内部实施拷贝动作.所以STL容器内存储的元素必须能够被拷贝(必 ...

  8. AngularJS进阶(十五)Cookie 'data' possibly not set or overflowed because it was too large

    Cookie 'data' possibly not set or overflowed because it was too large (5287 > 4096 bytes)! 注:请点击此 ...

  9. 【面试笔试算法】Program 6: 字符消除(hiho题库)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1) ...

  10. iOS监听模式系列之NSNotificationCenter的简单使用

    NSNotificationCenter 对于这个没必要多说,就是一个消息通知机制,类似广播.观察者只需要向消息中心注册感兴趣的东西,当有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象.这 ...