leetcode题目 寻找和为SUM的集合系列问题

该博客讨论了LeetCode中关于寻找和为特定数值的组合的两个题目。对于每个题目,博主给出了递归解决方案,但注意到这些方法在效率上不高,仅击败了少量提交的代码。内容包括题目描述、解题思路和测试结果,暗示可能有更优的算法来提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目一: 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, a1 ≤ a2 ≤ … ≤ 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]

思路: 递归解决,但是从最后提交的结果来看效率并不高,或许还有更好的算法?
代码:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        vector<vector<int>> result;
        vector<int> temp;

        for(auto i=candidates.begin();i!=candidates.end();++i)
        {
            temp.push_back(*i);
            FindSumSet(i,candidates.end(),result,temp,target-*i);
            temp.clear();
        }
        return result;
    }

    void FindSumSet(vector<int>::iterator begin,vector<int>::iterator end,vector<vector<int>>& result,vector<int> temp ,int target)
    {
        if(target==0)
        {
            result.push_back(temp);
            return;
        }
        else if(target<0)
            return;
        else
        {
            auto copy_temp=temp;
            for(auto i=begin;i!=end;++i)
            {
                copy_temp=temp;
                copy_temp.push_back(*i);
                FindSumSet(i,end,result,copy_temp,target-*i);
            }
        }
        return;
    }
};

测试结果: 虽然通过了,但是时间效率很差,只击败了6.17%的代码
这里写图片描述

题目二: 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, a1 ≤ a2 ≤ … ≤ 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]
思路: 只是比第一题复杂一点点,把第一题的代码改改就可以,但是也继承了第一题时间效率差的问题。

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(),candidates.end());
        vector<vector<int>> result;
        vector<int> temp;

        for(auto i=candidates.begin(),j=i;i!=candidates.end();i=j)
        {
            temp.push_back(*i);
            FindSumSet(i,candidates.end(),result,temp,target-*i);
            temp.clear();
            while(++j!=candidates.end()&&*j==*i);
        }
        return result;
    }
     void FindSumSet(vector<int>::iterator begin,vector<int>::iterator end,vector<vector<int>>& result,vector<int> temp ,int target)
    {
        if(target==0)
        {
            result.push_back(temp);
            return;
        }
        else if(target<0)
            return;
        else
        {
            auto copy_temp=temp;
            for(auto i=begin+1,j=i;i!=end;i=j)
            {
                copy_temp=temp;
                copy_temp.push_back(*i);
                FindSumSet(i,end,result,copy_temp,target-*i);
                while(++j!=end&*j==*i);
            }
        }
        return;
    }
};

测试结果: 虽然通过了,但是时间效率很差,只击败了10.71%的代码
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值