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 the 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]
]

这题用到 backtrack 方法, 需去重.

e.g.

A = [1 1 2 5 6 7 10], target = 8

正确输出应该是:

[[1,1,6],[1,2,5],[1,7],[2,6]]

难点在去重条件:
* 我写的,错误: `if(i >= 1 && A[i] == A[i - 1]) continue;`
- 错误输出: `[[1,2,5],[1,7],[2,6]]`
* 人家写的,正确: `if ((i >= 1) && (A[i] == A[i - 1]) && (i > start)) continue;`

差别就是 i > start 条件,挺不容易的想出来的.

自己想法,自个代码(被人家修正^^):

// backtrack
// A = [1 1 2 5 6 7 10]
vector<vector<int>> combinationSum2(vector<int>& A, int ta) {
sort(A.begin(), A.end());
vector < vector<int> > res;
vector<int> temp;
backtrack(A, res, temp, ta, 0);
return res;
} void backtrack(vector<int>& A, vector<vector<int> >& res, vector<int> temp,
int remain, int start) {
if (remain < 0)
return;
else if (remain == 0)
res.push_back(temp);
else {
for (int i = start; i < A.size(); i++) { // not correct: if(A[i] == A[i - 1] && i >= 1) continue;
// (i > start) is hard think, but important. if ((i >= 1) && (A[i] == A[i - 1]) && (i > start))
continue; // check duplicate combination temp.push_back(A[i]);
backtrack(A, res, temp, remain - A[i], i + 1); // i + 1, next element
temp.pop_back();
}
}
}

40. Combination Sum II(midum, backtrack, 重要)的更多相关文章

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

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

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

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

  3. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

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

  5. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. 40. Combination Sum II (JAVA)

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

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

  8. LeetCode OJ 40. Combination Sum II

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

  9. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

随机推荐

  1. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  2. 详解get请求和post请求参数中文乱码的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  3. Django实现 省 市 县 自关联的下拉级联

    前端部分: 三个下拉拉菜单进行级联 <body> <select id="province" > <option value="" ...

  4. linux下安装 配置 redis数据库

    通过终端命令安装(推荐): 1 确保更新源服务器能正常使用 如果没有更换更新源服务器,那么可能一直都下不了软件.欢迎参考我之前的博文来更换成国内的镜像服务器http://www.cnblogs.com ...

  5. 【Vue中的swiper轮播组件】

    <template> <swiper :options="swiperOption" ref="mySwiper"> <!-- s ...

  6. Tcl与Design Compiler (一)——前言

    已经学习DC的使用有一段时间了,在学习期间,参考了一些书,写了一些总结.我也不把总结藏着掖着了,记录在博客园里面,一方面是记录自己的学习记录,另一方面是分享给大家,希望大家能够得到帮助.参考的书籍有很 ...

  7. Java:日期类Date与Calendar

    Timestamp类型与日期类型之间的转化? Timestamp timestamp = Timestamp.valueOf("2017-03-17 07:00:00"); Dat ...

  8. spring boot 系列之三:spring boot 整合JdbcTemplate

    前面两篇文章我们讲了两件事情: 通过一个简单实例进行spring boot 入门 修改spring boot 默认的服务端口号和默认context path 这篇文章我们来看下怎么通过JdbcTemp ...

  9. packer的基本使用

    工具的产生,一定是为了解决某些痛点,那么痛点是? 你们在工作中是不是经常用到各种云?aliyun, aws, digitalOcean and so on? 你们的规模不大不小,经常去云平台上点一点, ...

  10. Eureka服务注册中心

    Eureka服务注册中心 最近在研究Spring Cloud,发现其中的组件实在是太多了,真的是头大,只能一块一块看,像盲人摸象一样.要想很短时间内掌握Spring Cloud是不可能的,小编就学习一 ...