LeetCode40 Combination Sum II
题目:
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. (Medium)
Note:
- All numbers (including target) will be positive integers.
- 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]
]
分析:
主体回溯框架和Combination Sum I一致,不同之处在于一个元素不能重复用,同时不能出现重复的组合。
所以DFS那一个条件 start 变为start + 1;
同时插入result之前判定是否出现过重复的结果。
代码:
class Solution {
private:
vector<vector<int>>result;
void dfs(int start, int end, const vector<int>& candidates, int target, vector<int>& internal) {
if (start > end) {
return;
}
if (candidates[start] == target) {
internal.push_back(candidates[start]);
if ( find(result.begin(), result.end(), internal) == result.end()) {
result.push_back(internal);
}
internal.pop_back();
return;
}
if (candidates[start] > target) {
return;
}
dfs(start + , end, candidates, target, internal);
internal.push_back(candidates[start]);
dfs(start + , end, candidates, target - candidates[start], internal);
internal.pop_back();
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
int end = candidates.size() - ;
vector<int> internal;
dfs(, end, candidates, target, internal);
return result;
}
};
LeetCode40 Combination Sum II的更多相关文章
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- leetcode-combination sum and combination sum II
Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...
随机推荐
- 使用gdb调试多线程程序总结
转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...
- mysql 全文查找fulltext
从 Mysql 4.0 开始就支持全文索引功能,但是 Mysql 默认的最小索引长度是 4.如果是英文默认值是比较合理的,但是中文绝大部分词都是2个字符,这就导致小于4个字的词都不能被索引,全文索引功 ...
- 数据库 CHECKDB 发现了x个分配错误和 x 个一致性错误
--1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...
- HDU 4612 Warm up(2013多校2 1002 双连通分量)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- linux 课后作业
第一章 第一单元 : 安装linux 系统:已完成 第二单元: 略 第三单元: 1) 要求以root用户登录系统,右击桌面打开终端,查看当前登陆Linux系统所使用的用户名2) 查看哪些用户在系统上工 ...
- 通过set和waitOne来控制子线程的运行和停止
public partial class Form1 : Form { //自动重置事件类 //主要用到其两个方法 WaitOne() 和 Set() , 前者阻塞当前线程,后者通知阻塞线程继续往下执 ...
- 将nginx配置为服务,php-fpm配置说明
编写shell脚本 vi /etc/init.d/nginx #!/bin/bash # # Startup script for the PHP-FPM server. # # chkconfig: ...
- logstash开源日志收集查询分析系统
http://storysky.blog.51cto.com/628458/1158707/ http://www.logstash.net/ http://blog.sina.com.cn/s/bl ...
- PetaPoco T4模板修改生成实体
PetaPoco T4 模板生成的实体类全部包含再一个.CS文件中.通过修改PetaPoco的T4模板,生成单文件实体. 1.生成单CS文件模板: SigleFile.ttinclude <#@ ...
- java使用org.apache.poi读取与保存EXCEL文件
一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...