LeetCode_Combination Sum

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:

  • 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]


题目要求找到满足: Sum = Target 条件的数字组合,并且相同的数字可以被多次使用,但是最终结果中要保证不能存在重复的组合。

分析:

1)这个题目可以用BFS来解,也可以使用DFS解,这里使用BFS求解;

2)BFS数据结构设定:

  队列:BFS 队列(Queue)用来存储当前的一个组合,队列中每个元素用vector<int>,每个元素的第一个元素表示当前组合的Sum。

3)剪枝条件:当前的Sum>target时,当前的分支就没有必要再词入队进行下次的搜索尝试了;

      当前正被考虑是否要加入到当前组合中的集合元素如果小于当前组合中的最大元素,也没有必要再尝试此分支;

求解过程优先要对给定集合candidates进行排序时间花费为O(nlogn);

然后进行BFS,BFS过程中每一步并没有对原有的问题规模进行实质性的减小,属于O(2^n)。

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<vector <int>> res;
        int sizeC=candidates.size();
        if(sizeC==0){
            return res;
        }
        queue < vector<int> > bfsQueue;
        
        //temp Ans tempAns[0] denote current sum
        vector <int> tempAns;
        sort(candidates.begin(), candidates.end(), less <int>() );
        //initialzie bfs
        for(int i=0;i<sizeC;i++){
            if(candidates[i]==target){
                res.push_back(vector<int>(1,candidates[i]));
                break;
            }
            bfsQueue.push(vector<int>(2,candidates[i]));
        }
        
        while(!bfsQueue.empty()){
            tempAns=bfsQueue.front();
            bfsQueue.pop();
            int maxC=tempAns[tempAns.size()-1];
            for(int i=0;i<sizeC;i++){
                if(maxC<=candidates[i]){
                    tempAns[0]+=candidates[i];
                    tempAns.push_back(candidates[i]);
                    //find one solution 
                    if(tempAns[0]==target){
                        tempAns.erase(tempAns.begin());
                        //judge if already in res
                        int k=0;
                        for(k=0;k<res.size();k++){
                            if(tempAns==res[k]){
                                break;
                            }
                        }
                        if(k==res.size()){
                            res.push_back(tempAns);
                        }
                        break;
                    }
                    else{
                        //keep try
                        if(tempAns[0]<target){
                            bfsQueue.push(tempAns);
                            tempAns.erase(tempAns.end()-1);
                            tempAns[0]-=candidates[i];
                        }
                    }
                }
            }
        }
        return res;
    }
};


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值