LeetCode: Combination Sum系列

77. Combinations


Given two integers n and k, return all possible combinations of k numbers out of 1 … n.

For example,
If n = 4 and k = 2, a solution is:

[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]


回溯


class Solution {
public:
    void tocom(vector<vector<int>> &res,int beg,int end,int k,vector<int> &v)
    {
        if(!k)  
        {
            res.push_back(v);
            return;
        }
        for(int i=beg;i<=end;++i)
        {
            vector<int> v1{v};
            v1.push_back(i);
            tocom(res,i+1,end,k-1,v1);
        }
    }
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> res;
        vector<int> v{};
        tocom(res,1,n,k,v);
        return res;
    }
};

39. 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.
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 search(vector<int>& candidates,vector<vector<int>> &res,int &i,int &target,int &sz,vector<int> v0)
    {
        for(int j=i;j<sz;++j)
        {
            int m=target-candidates[j];
           if(target>candidates[j])
           {
               if(m>=candidates[i]) 
               {
                   vector<int> v1{v0};
                   v1.push_back(candidates[j]);
                   search(candidates,res,j,m,sz,v1);
               }
           }
           else if(!m)
           {
              vector<int> v1{v0};
              v1.push_back(candidates[j]);
              res.push_back(v1);//出现能够符合条件的加入结果
               return;
           }
           else
           {
               return;
           }
        }
    }
    //总结:不要随意在结果集里面添加元素,想着以后再删,这样只会拉长时间,不如只添加合乎要求的。
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        sort(candidates.begin(),candidates.end());//可以及时停止循环
        int sz=candidates.size();
        for(int i=0;i<sz;++i)
        {
            int m=target-candidates[i];
            if(target>candidates[i])
            {
                if(m>=candidates[i]) 
                {
                    search(candidates,res,i,m,sz,{candidates[i]});
                }
            }
            else if(!m)
            {
                res.push_back({candidates[i]});
            }
            else
            {
                break;
            }
        }
        return res;
    }
};

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


res.assign(sv.begin(),sv.end());//仅顺序容器定义了assign(),这里学会了这个成员的用法。注意array没有定义。


class Solution {
public:
    void search(vector<int>& candidates,vector<vector<int>> &res,int i,int &target,int &sz,vector<int> v0)
    {
        for(int j=i;j<sz;++j)
        {
            int m=target-candidates[j];
           if(target>candidates[j])
           {
               if(m>=candidates[i]) 
               {
                   vector<int> v1{v0};
                   v1.push_back(candidates[j]);
                   search(candidates,res,j+1,m,sz,v1);
               }
           }
           else if(!m)
           {
              vector<int> v1{v0};
              v1.push_back(candidates[j]);
              res.push_back(v1);//出现能够符合条件的加入结果
               return;
           }
           else
           {
               return;
           }
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        sort(candidates.begin(),candidates.end());//可以及时停止循环
        int sz=candidates.size();
        for(int i=0;i<sz;++i)
        {
            int m=target-candidates[i];
            if(target>candidates[i])
            {
                if(m>=candidates[i]) 
                {
                    search(candidates,res,i+1,m,sz,{candidates[i]});
                }
            }
            else if(!m)
            {
                res.push_back({candidates[i]});
            }
            else
            {
                break;
            }
        }
        set<vector<int>> sv(res.begin(),res.end());//去重
        res.clear();
        res.assign(sv.begin(),sv.end());//仅顺序容器定义了assign()
        return res;
    }
};

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


class Solution {
public:
    void search(vector<vector<int>> &res,int beg,int k,int n,vector<int> v)
    {
        if((!k)&&(!n))
        {
            res.push_back(v);
            return;
        }
        for(int i=beg+1;i<=9&&i<=n;++i)
        {
            vector<int> v1{v};
            v1.push_back(i);
            search(res,i,k-1,n-i,v1);
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> res;
        vector<int> v{};
        search(res,0,k,n,v);
        return res;
    }
};

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 = 4

The 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?


回溯超时,对我而言里面最难的一个,动态规划,其实我想到了利用target以前数的状态,但是还是不知道如何写,后来参考别人代码写出。

https://discuss.leetcode.com/topic/52186/my-3ms-java-dp-solution


class Solution {
public:
    // int search(vector<int>& nums,int sz, int target)
    // {
    //     if(!target) return 1;
    //     if(target<0)    return 0;
    //     int res{0};
    //     for(int i=0;i<sz&&nums[i]<=target;++i)
    //     {
    //         res+=search(nums,sz,target-nums[i]);
    //     }
    //     return  res;
    // }

    int combinationSum4(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        vector<int> res(target+1,0);
        for(int i=1;i<=target;++i)
        {
            for(const int &im:nums)
            {
                if(im>i)
                {
                    break;
                }
                else if(i==im)
                {
                    res[i]+=1;//可能在此之前就已经有值
                }
                else
                {
                    res[i]+=res[i-im];//利用前面已求状态得到现在状态。
                }
            }
        }
        return res[target];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值