leetcode Combination Sum II回溯问题
2015-06-26 10:06
459 查看
最近在看回溯算法的框架,这个题代表性比较强,用到减枝(重复的数)和约束限制。
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
A solution set is:
回溯算法的基本框架,转自这个bolg 点击打开链接
其中,
参数
t>n 时,表示已搜索到一个叶节点,
这个题的主要问题就是重复的数字怎么办,首先进行排序是肯定的,然后需要引用一个辅助的数组,用于判断是否访问过,比如数组是1,1,2,6时候,有两个1,当我们要使用第二个1时,我们要检查他的前面一个1是否使用了,当未被使用时第二个1就不能使用。
解这个题有两个思路。
先画一个示意图,X表示不选,当到底部时就回溯,红笔部分代表需要剪纸的。
1.第一个思路比较好理解,就是对当前数有两个选择,选择或者不选。
,选择或者不选,要注意的地方是记得判断SUM<0,小于则直接return,否则会超时。
2.一般的框架
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,5and target
8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
回溯算法的基本框架,转自这个bolg 点击打开链接
[code]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; } };
相关文章推荐
- Android中Service与IntentService的使用比较
- Android中Service与IntentService的使用比较 2015-06-26 10:06 6人阅读 评论(0) 收藏
- [转]oracle中查询指定行数的记录
- 电脑全屏雪花飘落的背景特效
- Win10 手机预览版10149简单快速上手体验视频
- Java生成csv文件时,数字格式为“0001”,用excel打开时,变为“1”的解决方法
- Agilla中间件-mobile agent
- 黑马程序员——银行业务调度系统
- JVM系列文章(一):Java内存区域分析
- 向右滑动返回上一级界面
- php设计模式 (总结)
- mac 终端 常用命令
- thinkphp计划任务使用cronRun-Thinkphp3.1版
- Heat项目动态
- java读取wav文件(波形文件)并绘制波形图的方法
- 利用Android属性动画实现Banner的原理与实践
- hadoop
- Android:Layout_weight 随录
- Intent.ACTION_TIME_TICK 广播
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序