LeetCode:Combination Sum I II

Combination Sumhtml

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

The same repeated number may be chosen from C unlimited number of times.測試

Note:code

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]htm

 

簡單的回溯法(遞歸實現).blog

好比對於數組3,2,6,7,target = 7,對數組排序獲得[2,3,6,7]排序

一、第1個數字選取2, 那麼接下來就是解決從數組[2,3,6,7]選擇數字且target = 7-2 = 5遞歸

二、第2個數字選擇2,那麼接下來就是解決從數組[2,3,6,7]選擇數字且target = 5-2 = 3ip

三、第3個數字選擇2,那麼接下來就是解決從數組[2,3,6,7]選擇數字且target = 3-2 = 1leetcode

四、此時target = 1小於數組中的全部數字,失敗,回溯,從新選擇第3個數字

五、第3個數字選擇3,那麼接下來就是解決從數組[2,3,6,7]選擇數字且target = 3-3 = 0

六、target = 0,找到了一組解,繼續回溯尋找其餘解

 

須要注意的是:若是數組中包含重複元素,咱們要忽略(由於每一個數字能夠選擇屢次,若是不忽略的話,就會產生重複的結果)。貌似oj的測試集數組中都不包含重複的數字

 

class Solution {
private:
    vector<vector<int> > res;
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        sort(candidates.begin(), candidates.end());//爲了輸出結果遞增,所以先對數組排序
        vector<int> tmpres;
        helper(candidates, 0, target, tmpres);
        return res;
    }
    
    //從數組candidates[index,...]尋找和爲target的組合
    void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
    {
        if(target == 0)
        {
            res.push_back(tmpres);
            return;
        }
        for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
            if(i == 0 || candidates[i] != candidates[i-1])//因爲每一個數能夠選取屢次,所以數組中重複的數就不用考慮
            {
                tmpres.push_back(candidates[i]);
                helper(candidates, i, target - candidates[i], tmpres);
                tmpres.pop_back();
            }
    }
};

 


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:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • 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]

 

和上一題差很少,只是每一個元素只能選一次。

因爲有重複元素的存在,好比數組爲[1(1),1(2),2,3],target = 6. 可能出現重複結果1(1),2,3 和 1(2),2,3                          本文地址

咱們能夠以下處理:若是數組中當前的數字出現重複,在前面重複了k次,且臨時結果數組中也包含了k個當前數字,那麼當前的數字能夠選擇;不然就不選擇當前數字

class Solution {
private:
    vector<vector<int> >res;
public:
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<int> tmpres;
        helper(candidates, 0, target, tmpres, 0);
        return res;
    }
    
    //從數組candidates[index,...]尋找和爲target的組合,times爲前一個數字candidates[index-1]重複出現的次數
    void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres, int times)
    {
        if(target == 0)
        {
            res.push_back(tmpres);
            return;
        }
        for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
        {
            if(i > 0 && candidates[i] == candidates[i-1])times++;
            else times = 1;
            if(times == 1 || (tmpres.size() >= times-1 && tmpres[tmpres.size()-times+1] == candidates[i]))
            {
                tmpres.push_back(candidates[i]);
                helper(candidates, i+1, target - candidates[i], tmpres, times);
                tmpres.pop_back();
            }
        }
    }
};

 

還有一種方法是,在每一個子問題的數組中,重複的數字都不選擇,這種更簡潔

class Solution {
private:
    vector<vector<int> >res;
public:
    vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<int> tmpres;
        helper(candidates, 0, target, tmpres);
        return res;
    }
    
    //從數組candidates[index,...]尋找和爲target的組合
    void helper(vector<int> &candidates, const int index, const int target, vector<int>&tmpres)
    {
        if(target == 0)
        {
            res.push_back(tmpres);
            return;
        }
        for(int i = index; i < candidates.size() && target >= candidates[i]; i++)
        {
            if(i > index && candidates[i] == candidates[i-1])continue;//當前子問題中,重複數字都不選擇
            tmpres.push_back(candidates[i]);
            helper(candidates, i+1, target - candidates[i], tmpres);
            tmpres.pop_back();
        }
    }
};

 

【版權聲明】轉載請註明出處:http://www.cnblogs.com/TenosDoIt/p/3802647.html

相關文章
相關標籤/搜索