leetcode 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.
  • Elements in a combination (a1a2, … , 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] 

回溯算法的基本框架,转自这个bolg 点击打开链接

void backtrack(int t)
{
    if(t > n) output(x);
    else
        for (int i = f(n,t); i <= g(n,t); ++i) {
            x[t] = h(i);
            if(constraint(t) && bound(t)) backtrack(t+1);
        }
}

其中,

  • 参数 t 表示递归深度,即当前扩展节点在解空间树中的深度,
  • n 解空间树的高度,当 t>n 时,表示已搜索到一个叶节点,
  • output(x) 打印可行解,
  • f(n,t) 和 g(n,t) 分别表示当前扩展节点处子树的起止编号,
  • h(i) 表示当前扩展节点处 x[t] 的第i个可选值,
  • constraint(t) 和 bound(t) 分别为约束函数和限界函数,用于剪枝。
这个题的主要问题就是重复的数字怎么办,首先进行排序是肯定的,然后需要引用一个辅助的数组,用于判断是否访问过,比如数组是1,1,2,6时候,有两个1,当我们要使用第二个1时,我们要检查他的前面一个1是否使用了,当未被使用时第二个1就不能使用。
解这个题有两个思路。
先画一个示意图,X表示不选,当到底部时就回溯,红笔部分代表需要剪纸的。


1.第一个思路比较好理解,就是对当前数有两个选择,选择或者不选。
class Solution {
public:
	//排列问题,选择或者不选
	vector<vector<int>> res;
	vector<int>temp;
	vector<int>a;//a是一个辅助的数组,用于判断是否使用该
	void backtracking(int dep, vector<int>&candidates, int sum)
	{
		if(sum<0) return;
		if (sum == 0)//到达深度了
		{
			res.push_back(temp);
			return;
		}
		if (dep == candidates.size())//不能越界
		{
			return;
		}
		temp.push_back(candidates[dep]);//选择,则target+1,dep+1
		a[dep]++;
		if (dep>0 && candidates[dep] == candidates[dep - 1] && a[dep - 1] == 0 && a[dep]!=0)
		//当我们要使用第二个1时,我们要检查他的前面一个1是否使用了,当未被使用时第二个1就不能使用。
		{
			//剪枝,还要继续回退,只不过不继续向下
			temp.pop_back();//不选择,回退,深度加1
			a[dep]--;//切记a也要--
			backtracking(dep + 1, candidates, sum);
		}
		else
		{
			backtracking(dep + 1, candidates, sum - candidates[dep]);
			temp.pop_back();//不选择,回退,深度加1
			a[dep]--;
			backtracking(dep + 1, candidates, sum);
		}
	}
	vector<vector<int>> combinationSum2(vector<int>& candidates, int sum) {
		sort(candidates.begin(), candidates.end());
		a.resize(candidates.size());
		backtracking(0, candidates, sum);
		return res;
	}
};

,选择或者不选,要注意的地方是记得判断SUM<0,小于则直接return,否则会超时。
2.一般的框架
class Solution {
public:
	//排列问题,选择或者不选
	vector<vector<int>> res;
	vector<int>temp;
	vector<int>a;//a是一个辅助的数组,用于判断是否使用
	void backtracking(int dep, vector<int>&candidates, int sum)
	{
		if (sum<0)
		{
			return;
		}
		if (sum == 0)//到达深度了
		{
			res.push_back(temp);
			return;
		}
		for (int i = dep; i < candidates.size(); i++)
		{
			temp.push_back(candidates[i]);
			a[i]++;
			if (i>0 && candidates[i] == candidates[i - 1] && a[i - 1] == 0 && a[i] != 0)
			{
				temp.pop_back();
				a[i]--;
				continue;
			}
			backtracking(i+1, candidates, sum-candidates[i]);
			temp.pop_back();
			a[i]--;
		}
	}
	vector<vector<int>> combinationSum2(vector<int>& candidates, int sum) {
		sort(candidates.begin(), candidates.end());
		a.resize(candidates.size());
		backtracking(0, candidates, sum);
		return res;
	}
};



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值