[LeetCode] Combination Sum (bfs)

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]

方法:用queue实现bfs

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<vector<int> > result;

        if(candidates.size()==0){
          vector<int> tmp;
           result.push_back(tmp);
           return result;
        }
        sort(candidates.begin(),candidates.end());

        bfs(result,candidates,target);
        return result;
    }//end func

private:
    void bfs(vector<vector<int> > &result,vector<int> &candidates,int target){
        queue<vector<int> > q;
        int len = candidates.size();
        for(int i = 0;i<len;i++){
            int sum = 0;
            int value = candidates[i];
            vector<int> tmp;
            while(true){
                sum += value;
                tmp.push_back(value);
                if(sum<target){
                   q.push(tmp);
                }else if(sum == target){
                   if(find(result.begin(),result.end(),tmp)==result.end())
                       result.push_back(tmp);
                }
                else
                    break;
            }

            while(!q.empty()){
                tmp = q.front();
                q.pop();
                sum = 0;
                for(int k=0;k<tmp.size();k++){
                    sum += tmp[k];
                }
                int sum0 = sum;
                vector<int> tmp0(tmp);
                for(int j=i+1;j<len;j++){
                    value = candidates[j];
                    while(true){
                        sum += value;
                        tmp.push_back(value);
                        if(sum<target){
                            q.push(tmp);
                        }else if(sum == target){
                            sort(tmp.begin(),tmp.end());
                            if(find(result.begin(),result.end(),tmp)==result.end())
                                result.push_back(tmp);
                        }
                        else
                            break;
                    }
                    sum = sum0;
                    tmp = tmp0;
                }
            }
        }//end for
    }//end func
};
时间: 2024-07-31 14:26:46

[LeetCode] Combination Sum (bfs)的相关文章

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode: Combination Sum [038]

[题目] 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)

LeetCode: Combination Sum II [039]

[题目] 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

Leetcode:Combination Sum 子集和问题

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 (includ

Leetcode | Combination Sum I &amp;&amp; II

Combination Sum I 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 (includ

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution 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

LeetCode: Combination Sum II 解题报告

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 ta

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

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1