博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Combination Sum II
阅读量:4150 次
发布时间:2019-05-25

本文共 3023 字,大约阅读时间需要 10 分钟。

class Solution {	//DFS	//sort candidates first and then DFS	//note here how to get the unique combination: !(index != 0 && candidates[index] == candidates[index-1] && !used[index-1])	//and the cut off trick: sumNow+candidates[index] <= targetpublic:	vector
> combinationSum2(vector
&candidates, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(candidates.begin(), candidates.end()); vector
> ans; vector
path; vector
used(candidates.size(), false); combinationSum_aux(0, 0, target, path, ans, candidates, used); return ans; } void combinationSum_aux( int index, int sumNow, int target, vector
& path, vector
>& ans, vector
& candidates, vector
& used ) { //throw std::exception("The method or operation is not implemented."); if(sumNow == target) { ans.push_back(path); return; } if(index >= candidates.size()) return; if( !(index != 0 && candidates[index] == candidates[index-1] && !used[index-1]) && sumNow+candidates[index] <= target) {//cut off here is very important used[index] = true; path.push_back(candidates[index]); combinationSum_aux(index+1, sumNow+candidates[index], target, path, ans, candidates, used); path.pop_back(); used[index] = false; } combinationSum_aux(index+1, sumNow, target, path, ans, candidates, used); }};

second time

class Solution {public:    void combinationSumUtil(vector
& candidates, int target, int curIdx, vector
& curPath, vector
& used, vector
>& allPath) { if(target == 0) { allPath.push_back(curPath); return ; } if(target < 0) return; if(curIdx >= candidates.size()) return; if(curIdx >= 1 && candidates[curIdx] == candidates[curIdx-1] && used[curIdx-1] == false) combinationSumUtil(candidates, target, curIdx+1, curPath, used, allPath); else { curPath.push_back(candidates[curIdx]); used[curIdx] = true; combinationSumUtil(candidates, target-candidates[curIdx], curIdx+1, curPath, used, allPath); used[curIdx] = false; curPath.pop_back(); combinationSumUtil(candidates, target, curIdx+1, curPath, used, allPath); } } vector
> combinationSum2(vector
&candidates, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(candidates.begin(), candidates.end()); vector
curPath; vector
> allPath; vector
used(candidates.size(), false); combinationSumUtil(candidates, target, 0, curPath, used, allPath); return allPath; }};

转载地址:http://soxti.baihongyu.com/

你可能感兴趣的文章
在osg场景中使用GLSL语言——一个例子
查看>>
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>
49个在工作中常用且容易遗忘的CSS样式清单整理
查看>>
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>