Combination Sum II

题目:

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:

  1. All numbers (including target) will be positive integers.

  2. Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).

  3. 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]

题意:给定一个数组C和一个特定值T,要求找出这里面满足以下条件的所有答案:数组中数字的值加起来等于特定和的答案.并且数组中的数字不允许被使用多次,但如果一开始就存在多个的话,可以使用多次。


分析:

   可以采用与Combination Sum 类似的方法,但是由于题二中的每个数只能取一次,所以dfs函数中的for循环,i应从l+1开始,表示取下一个数。但这样带来的问题是,结果中会出现重复的取数方案,拿上面的例子来分析:C中有两个1可以选,那第一个1和7是一种可选方案(1+7=8),第二个1和7也是一种可选方案,按照上述算法,[1,7]会在结果中出现两次。当然可以对最后结果去重(如果用C++的话,sort->unique->erase可以实现)。不幸的是,这种解法会超时。解决超时的方案是不要将重复的方案加入到结果集中,也就避免了去重的工作。

<pre name="code" class="java">public class Solution {
  public List<List<Integer>> combinationSum2(int[] num, int target) {
    LinkedList<List<Integer>> res=new LinkedList<List<Integer>>();
    LinkedList<Integer> list=new LinkedList<Integer>();
    if(num==null || num.length<=0) return res;
    Arrays.sort(num);
    dfs(0,0,target,num,list,res);
    HashSet set=new HashSet(res);
    res.clear();
    res.addAll(set);
    return res;
  }
  public void dfs(int start,int sum,int target,int[] num,LinkedList<Integer> list,List<List<Integer>> res){
    if(sum==target){
      LinkedList<Integer> l=new LinkedList<Integer>(list);
      res.add(l);
      return;
    }
    for(int i=start;i<num.length;i++){
      if(sum+num[i]>target) return;
      list.add(num[i]);
      dfs(i+1,sum+num[i],target,num,list,res);
      list.pollLast();
    }
  }
}


 

 

 注意:用set剔除重复的组。 

c++:

class Solution {
public:
    void internalCombinationSum2(vector<int> &num,
            int start,
            int sum,
            int target,
            vector<int> &combination,
            vector<vector<int> > &result) {
        int size = num.size();
        
        if (sum == target) {
            result.push_back(combination);
            
            return;
        }
        else if ((start >= size) || (sum > target)) {
            return;
        }

        for (int i = start; i < size; ) {
            combination.push_back(num[i]);
            internalCombinationSum2(num, i + 1, sum + num[i], target, combination, result);
            combination.pop_back();
            
            int j = i + 1;
            
            while (j < size) {
                if (num[i] == num[j]) {
                    ++j;
                }
                else {
                    break;
                }
            }
            
            i = j;
        }
    }
    
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        vector<vector<int> > result;
        vector<int> combination;
        
        sort(num.begin(), num.end());
        internalCombinationSum2(num, 0, 0, target, combination, result);
           
        return result;
    }
};

http://www.cnblogs.com/panda_lin/archive/2013/11/05/combination_sum_ii.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值