[leetcode]Combination Sum II
2015-10-12 17:24
405 查看
class Solution { public: void help(vector<int> &v,int now,int sum,int target,vector<int> &path,vector<vector<int> >&ans,bool last){ //now表示当前的数 //sum表示当前所选的数的和 //target表示目标数据 //path表示所有已选的数 //last上一个数是否选择 //相同的数连续取,不能跳着取 if(sum>target){ return; } if(now>=v.size()){ if(sum==target){//组合完成 ans.push_back(path); } return; } if((now==0)||(v[now-1]!=v[now])||last){//第一个必须取;当前数和前一个数不一样;当前数和前一个一样,并且取了前一个数 path.push_back(v[now]); help(v,now+1,sum+v[now],target,path,ans,true); path.pop_back();//回复path } help(v,now+1,sum,target,path,ans,false);//不取当前数 } vector<vector<int>> combinationSum2(vector<int>& candidates, int target) { sort(candidates.begin(),candidates.end());//排序 vector<int> path;//已选的数 vector<vector<int> > ans;//结果 help(candidates,0,0,target,path,ans,true); return ans; } };
相关文章推荐
- 在Bilibili/ijkplayer,里面增加librtmp。
- xshell联系CentOS6.5 iptables要么ls 乱码输出
- mysql慢查日志分析工具 percona-toolkit
- sql存储过程
- Java web小记
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 使用bat文件备份数据库
- 第四周 项目6 多项式求和
- 第六周项目5--后缀表达式
- 第六周 项目三-括号的匹配
- 项目3 -- 括号的匹配 顺序栈方法 .