[LeetCode]39 和的组合形式

本文介绍了一种使用回溯法解决组合总和问题的方法。给定一个正整数数组C和目标值T,找出所有使得组合之和等于T的不重复组合。文章详细解释了解题思路,并提供了两种不同的C++实现方式。

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

Combination Sum(和的组合形式)

【难度:Medium】
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]
给定一个正整数数组C和目标值T,在C中找到所有不重复的数字组合(组合不完全相同,同一数字可能在一个组合中多次),使组合的和都是T,以非降序的形式。如C = [2,3,6,7], T = 7,解集为[7]和[2,2,3]。


解题思路

此题可以使用回溯法来解决:假设当前的数是某个组合中的一个,从这一个状态出发,搜索从这一个状态可能到达的所有状态,直到进入“死胡同”或得到一个解。再后退一步或若干步,直到所有可能都已试探过。


c++代码如下:
解法一:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        vector<int> tmp;
        if (candidates.empty())
            return ans;
        //求解之前先对数组升序排序
        quicksort(candidates,0,candidates.size()-1);
        if (target < candidates[0])
            return ans;
        //从第一个数开始递归回溯
        backtracking(ans,candidates,tmp,target,0);
        return ans;
    }
    void quicksort(vector<int>& n, int low, int high) {
        if (low >= high)
            return;
        int i = low;
        int j = high;
        int k = n[low];
        while (i < j) {
            while (i < j && n[j] >= k)
                j--;
            n[i] = n[j];
            while (i < j && n[i] <= k)
                i++;
            n[j] = n[i];
        }
        n[i] = k;
        quicksort(n,low,i-1);
        quicksort(n,i+1,high);
    }
    //tmp保留中间状态,ans保留最终结果,t是要组成的和,index是目前访问数组的下标
    void backtracking(vector<vector<int>>& ans, vector<int> n, vector<int>& tmp, int t, int index) {
        //边界条件,t等于0时意味着求到一个解
        if (t == 0) {
            if (tmp.empty())
                return;
            ans.push_back(tmp);
            return;
        }
        //t小于0则不满足一个解
        if (t < 0) {
            return;
        }
        //搜索到尽头同样不是解
        if (index == n.size())
            return;
        //组合中包含当前的数字
        tmp.push_back(n[index]);
        //进行新的递归
        backtracking(ans,n,tmp,t-n[index],index);
        //回退
        tmp.pop_back();
        //试探下一个数
        backtracking(ans,n,tmp,t,index+1);
        return;
    }

};

解法二:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> ans;
        vector<int> tmp;
        if (candidates.empty())
            return ans;
        quicksort(candidates,0,candidates.size()-1);
        if (target < candidates[0])
            return ans;
        backtracking(ans,candidates,tmp,target,0);
        return ans;
    }
    void quicksort(vector<int>& n, int low, int high) {
        if (low >= high)
            return;
        int i = low;
        int j = high;
        int k = n[low];
        while (i < j) {
            while (i < j && n[j] >= k)
                j--;
            n[i] = n[j];
            while (i < j && n[i] <= k)
                i++;
            n[j] = n[i];
        }
        n[i] = k;
        quicksort(n,low,i-1);
        quicksort(n,i+1,high);
    }
    void backtracking(vector<vector<int>>& ans, vector<int> n, vector<int>& tmp, int t, int index) {
        if (t == 0) {
            ans.push_back(tmp);
            return;
        }
        //与解法一的区别在于将一的尾部递归换成了while循环来表达,这种方式的耗时更低,因为递归的效率没有循环好
        while (index < n.size()) {
            if (t < n[index])
                break;
            if (t == n[index] || t >= 2*n[index]) {
                tmp.push_back(n[index]);
                backtracking(ans,n,tmp,t-n[index],index);
                tmp.pop_back();
            }
            index++;
        }
        return;
    }

};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值