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 ...
随机推荐
- js运动 淡入淡出
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- XE10 Seattle error___seh_personality_v0
Seattle bcc32c compiler error [ilink32 Error] Error: Unresolved external '___seh_personality_v0' ref ...
- Hadoop概念学习系列之hadoop生态系统闲谈(二十五)
分层次讲解 最底层平台 ------->hdfs yarn mapreduce spark 应用层-------->hbase hive pig sparkSQL nu ...
- poj 2239 Selecting Courses(二分匹配简单模板)
http://poj.org/problem?id=2239 这里要处理的是构图问题p (1 <= p <= 7), q (1 <= q <= 12)分别表示第i门课在一周的第 ...
- 用UltraISO制作的u盘ubuntu11.04,启动失败解决方案
错误提示:SYSLINUX 3.84 2009-12-18 EBIOS Copyright c 1994-2009 H.Peter Anvin et al 折腾的很久,尝试用Pauly的bootice ...
- String.valueOf(null) 报空指针
String.valueOf 默认的方法 argument 可以为null 的 boolean b = null; char c = null; char[] data = null; double ...
- Joel Spolsky对计算机学生的七大建议
/*先来介绍下作者:Joel Spolsky,世界最具影响的程序员网志Joel on Software的主人,软件业一位旗帜鲜明的思想者,一位传统软件管理理念的挑战者.他创办的这个网站被程序员誉为“反 ...
- Spring REST实践之Versioning,Paging和Sorting
Versioning 为适应需求的变化以及兼容已有的API,需要创建新版本的API,一般有四种流行的版本化API的方法: URI版本化 URI参数版本化 Accept header版本化 自定义hea ...
- JAX-RS入门 一 :基础
简介 JAX-RS是一套用java实现REST服务的规范,提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源.标注包括: @Path,标注资源类或方法的相对路径 @GET,@PUT, ...
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...