LintCode: Combination Sum II
2015-12-02 13:56
274 查看
C++
DFS
DFS
class Solution { public: void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) { if (sum > target) { return ; } if (now >= a.size()) { if (sum == target) { ans.push_back(path); } return ; } if ((now == 0) || (a[now - 1] != a[now]) || last) { path.push_back(a[now]); help(a, now + 1, sum + a[now], target, path, ans, true); path.pop_back(); } help(a, now + 1, sum, target, path, ans, false); } /** * @param num: Given the candidate numbers * @param target: Given the target number * @return: All the combinations that sum to target */ vector<vector<int> > combinationSum2(vector<int> &num, int target) { // write your code here sort(num.begin(), num.end()); vector<int> path; vector<vector<int> > ans; help(num, 0, 0, target, path, ans, true); return ans; } };
相关文章推荐
- 版本控制---关于GIT(2)
- Material Design 摘要
- Python 效率
- Java常用的设计模式14:常用设计模式之组合模式(结构型模式)
- android Studio常用快捷键
- Windows 删除共享映射
- android适配(一)
- C语言_数组与指针
- 关于Html中文乱码的问题
- 关于android.support.v7.widget.RecyclerView包导入项目引发的BUG
- Codeforces Round #334 (604B) More Cowbell [贪心]
- InnoDB Monitor
- UItableView分割线根据文本内容显示
- 问题2-:Syntax error on tokens, delete these tokens
- NuGet之控制台管理程序包
- php过滤函数
- HMM学习笔记
- 父页面打开子页面的同时传递值
- jQuery事件绑定的最佳实践
- PAT 1093. Count PAT's (25)