博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
力扣算法题—040组合求和二
阅读量:7205 次
发布时间:2019-06-29

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

1 #include "000库函数.h" 2  3 //第一感觉使用回溯比较快 4 //96ms 5  6  7 class Solution { 8 public: 9     vector
> combinationSum2(vector
& candidates, int target) {10 vector
>R;11 sort(candidates.begin(), candidates.end());12 if (candidates.size() == 0)return R;13 vector
v;//临时存放解 14 Combin(candidates, R, v, target, 0, 0); 15 return R;16 }17 18 void Combin(vector
candidates, vector
>&Res, vector
&v, int target, int start, int sum) {19 if (sum == target) {20 //sort(v.begin(), v.end());21 Res.push_back(v);22 return;23 }24 else if (sum > target)25 return;26 27 for (int i = start; i < candidates.size(); ++i) {28 if (i > start&&candidates[i] == candidates[i - 1])continue;//去除重复的组合 29 v.push_back(candidates[i]);30 Combin(candidates, Res, v, target, i + 1, sum + candidates[i]);//i+1是与上题的不同之处,不会出现重复使用元素31 v.pop_back();//将数字退出 32 }33 }34 35 };36 37 void T040() {38 vector
v;39 vector
>Res;40 Solution s;41 v = { 10,1,2,7,6,1,5 };42 Res = s.combinationSum2(v, 8);43 for (auto &a : Res) {44 for (auto b : a)45 cout << b << " ";46 cout << endl;47 }48 cout << endl << "*********" << endl;49 /*v = { 2, 3,5 };50 Res = s.combinationSum(v, 8);51 for (auto &a : Res) {52 for (auto b : a)53 cout << b << " ";54 cout << endl;55 }56 cout << endl << "*********" << endl;57 */58 59 60 }

 

转载于:https://www.cnblogs.com/zzw1024/p/10566913.html

你可能感兴趣的文章
GitHub上最火的74个Android开源项目(三)
查看>>
NYOJ-86 找球号(一)AC 分类: NYOJ ...
查看>>
ios开发日记-1 设置UIImage的渲染模式:UIImage.renderingMode
查看>>
Codewars-Javascript训练手册:字符串(中)
查看>>
远离淫妇
查看>>
sql中in和exist语句的区别?(补充了left join和right join)
查看>>
NSString方法compare详解
查看>>
js getByClass函数封装
查看>>
Java EE之JSP
查看>>
ztree数据库交互
查看>>
前端开发者基本要求---转
查看>>
自动化测试学习笔记
查看>>
怎样计算页面执行的时间?
查看>>
Linux常用压缩命令
查看>>
POJ 2676, Sudoku
查看>>
MySQL引擎
查看>>
Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties
查看>>
AIM Tech Round 5 (rated, Div. 1 + Div. 2) D(SET,思维)
查看>>
连接远程docker内的mysql(navicat)
查看>>
使用border实现提示框的
查看>>