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]

解题思路:

求序列的排列组合,典型的回溯算法思想。

如果有兴趣查看Combination Sum版本I,请点击链接。

版本一中candidate没有重复,但是可以允许重复使用candidates中的元素;即,对于{1,2,3,4},选择了1,还可以继续在{1,2,3,4}中选择,返回序列不会出现重复结果;

版本二(本题)设置candidate有重复,但是每个candidate只能使用一次;即,对于{1,2,2,3,4},选择了1,只能继续在{2,2,3,4}中选择;

  但是,因为candidate中“2”有重复,如果需要选择“2”,只能选择“第一个2”,不能选择“第二个2”;

  因为如果target是10,选择第一个2会返回结果{1,2,3,4},但是选择第二个2,也会出现相同序列{1,2,3,4};造成返回队列包含重复结果;

  选择“第一个2”后,还能继续从{2,3,4}中挑选下一个数,因此不会漏选;

代码:

 class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> lst;
vector<int> ans;
sort(candidates.begin(), candidates.end());
Backtracking(lst, ans, candidates, , target);
return lst;
} void Backtracking(vector<vector<int>> &lst, vector<int> ans, vector<int> candidates, int idx, int left) {
for (int i = idx; i < candidates.size(); ++i) {
if (i > idx && candidates[i] == candidates[i-])
continue;
int cur_v = left - candidates[i];
if (cur_v == ) {
ans.push_back(candidates[i]);
lst.push_back(ans);
return;
} if (cur_v > ) {
ans.push_back(candidates[i]);
Backtracking(lst, ans, candidates, i + , cur_v);
ans.pop_back();
} else {
break;
}
}
return;
}
};

【Leetcode】【Medium】Combination Sum II的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  3. 【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 ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  8. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  9. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  10. 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) ...

随机推荐

  1. EC20的短消息

    一 设置短消息的操作模式:AT+CMGF=<mode>  0=PDU,固定的16进制信息:=1文本模式. 二选择TE 字符集(+CSCS )AT+CSCS=<chset>字符集 ...

  2. dubbo示例

    Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 我也不明白这是什么意思,使用了之后大概就是提供一个将多个项目进行联合的一种分布式,使用的是一 ...

  3. 2.1 js 基础--select深入

    select     获取select下的所有option     用op=select.options         option属性:op[1].value,op[1].text;     // ...

  4. Java生成二维码和解析二维码URL

    二维码依赖jar包,zxing <!-- 二维码依赖 start --><dependency> <groupId>com.google.zxing</gro ...

  5. Scrapy框架学习(一)Scrapy框架介绍

    Scrapy框架的架构图如上. Scrapy中的数据流由引擎控制,数据流的过程如下: 1.Engine打开一个网站,找到处理该网站的Spider,并向该Spider请求第一个要爬取得URL. 2.En ...

  6. VMware workstation 虚拟机安装Windows Server 2008 r2

    问题秒速: VMware workstation 虚拟机安装Windows Server 2008 r2,配置好参数后,选择开机,报错,错误如图:

  7. show_space1

    CREATE OR REPLACE PROCEDURE show_space1(p_segname IN VARCHAR2, p_owner IN VARCHAR2 DEFAULT 'NTSUSER0 ...

  8. NoSQL集锦

    1. http://blog.nosqlfan.com/,有不少Redis.CouchDB.MongoDB的电子书和文章,但没有Memcached的.

  9. CSS 文字超长省略显示并隐藏超长部分

    1.包含文字的元素必须是块级元素,不是块级元素使用display:block使其具有块级元素属性: 2.具备上述基本条件后,css样式如下: { display: block; max-width: ...

  10. [android] 练习使用ListView(三)

    解决OOM和图片乱序问题 package com.android.test; import java.io.InputStream; import java.net.HttpURLConnection ...