leetcode 39|40|216|377. Combination Sum 1|2|3|4

来源:互联网 发布:php短信轰炸机 编辑:程序博客网 时间:2024/06/10 04:29

39. Combination Sum

Given a set of candidate numbers (C) (without duplicates) 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.
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]
]

使用回溯法

class Solution {public:    void combine(vector<int>& cand, vector<vector<int>> &result, vector<int> path, int pos, int base, int target)    {        if (base == target)        {            result.push_back(path);            return;        }        else if (base > target)            return;        else        {            for (int i = pos; i < cand.size(); i++)            {                path.push_back(cand[i]);                combine(cand, result, path, i, base+cand[i], target);                path.pop_back();            }        }    }            vector<vector<int>> combinationSum(vector<int>& cand, int target)     {        sort(cand.begin(), cand.end());        vector<vector<int>> result;        vector<int> path;        combine(cand, result, path, 0, 0, target);        return result;    }};


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

题目:难点在于怎么去重
方法:backtracking

class Solution {public:    void combine(vector<vector<int>> &result,vector<int>& candi,vector<int> path, int pose,int base,int target)    {        if (base == target)        {            result.push_back(path);            return;        }        else if (base > target)            return;        else        {            for(int i = pose; i < candi.size(); i++)            {                if(i - 1 >= pose && candi[i] == candi[i-1])  //i-1>=pose是一个关键点,避免重复!                    continue;                    path.push_back(candi[i]);                combine(result, candi, path, i + 1, base + candi[i], target);                path.pop_back();            }        }    }        vector<vector<int>> combinationSum2(vector<int>& candi, int target)     {        sort(candi.begin(),candi.end());        vector<vector<int>> result;            vector<int> path;            combine(result, candi, path, 0, 0, target);               return result;     }};

216. 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.

Example 1:

Input:  k = 3,  n = 7

Output: 

[[1,2,4]]

Example 2:

Input:  k = 3,  n = 9

Output: 

[[1,2,6], [1,3,5], [2,3,4]]

题目:找1-9中k个数凑成n
思路:由于k不定,只能递归


class Solution {public:        void combine(vector<vector<int>> & ret, vector<int> m1, int target, int i, int k)    {        if (target > k * 9 || k == 0)            return;                if (k == 1 && target >= i)        {            m1.push_back(target);            ret.push_back(m1);            return;        }        else if(k == 1 && target < i)            return;        else //k>1 k代表还剩几个数来凑        {            for (int j = i; j <= 9; j++)            {                m1.push_back(j);                combine(ret, m1, target - j, j + 1, k - 1);                m1.pop_back();            }        }    }        vector<vector<int>> combinationSum3(int k, int n)     {        vector<vector<int>> ret;                 if (n > k * 9)            return ret;                 vector<int> m1;        combine(ret, m1, n, 1, k);            return ret;        }};


377. Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]target = 4The possible combination ways are:(1, 1, 1, 1)(1, 1, 2)(1, 2, 1)(1, 3)(2, 1, 1)(2, 2)(3, 1)Note that different sequences are counted as different combinations.Therefore the output is 7.

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

1、这种求有多少种肯定是动态规划,求具体的种类就是回溯。

2、dp[target] = sum(dp[target-nums[i]]) (for i=0..num.size()-1)
dp[target] 代表一共有多少种可能。

class Solution {public:    int combinationSum4(vector<int>& nums, int target)     {        vector<int> ret(target + 1, 0);        ret[0] = 1;        for (int i = 0; i <= target; i++)        {            for (auto it : nums)            {                if (it <= i)                    ret[i] += ret[i - it];             }        }        return ret[target];    }};







原创粉丝点击