博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Combination-Sum II
阅读量:4338 次
发布时间:2019-06-07

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

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,5 and target 8, 
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

1 class Solution { 2 public: 3     vector
> combinationSum2(vector
&num, int target) { 4 sort(num.begin(), num.end()); 5 vector
> res; 6 vector
com; 7 combinationSum2(num, target, res, com, 0); 8 return res; 9 }10 11 void combinationSum2(vector
& num, int target, vector
> &res, vector
&com, int start)12 {13 if(target == 0) {14 res.push_back(com);15 return;16 }17 for(int i = start; i < num.size() && num[i] <= target; i++) {18 if(i > start && num[i] == num[i-1]) continue; // !duplicate cases19 com.push_back(num[i]);20 combinationSum2(num, target-num[i], res, com, i+1);21 com.pop_back();22 }23 }24 };

 

转载于:https://www.cnblogs.com/zhengjiankang/p/3646360.html

你可能感兴趣的文章
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>