作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/combination-sum-ii/description/

题目描述

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

  1. All numbers (including target) will be positive integers.
  2. The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]

题目大意

使用候选集的数字,能有多少种不同的组合,使得每个组合的和都是target。但是这里给出的数字有重复,要求结果里面,相同的组合只能出现一次。

解题方法

方法一:DFS

这个题和之前的39. Combination Sum 基本相同,这个题不允许一个数字多次出现,所以每次递归需要比上一轮开始的位置向后移动一个。

另外这个题一直做不出来的原因是把dfs的i写成了index…要注意内层递归的时候,传入的位置是i不是index.

输入:

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

结果:

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

class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort()
print(candidates)
res = []
self.dfs(candidates, target, 0, res, [])
return res def dfs(self, nums, target, index, res, path):
if target < 0:
return
elif target == 0:
res.append(path)
return
for i in xrange(index, len(nums)):
if i > index and nums[i] == nums[i-1]:
continue
self.dfs(nums, target - nums[i], i + 1, res, path + [nums[i]])

方法二:回溯法

这个题的回溯法也很简单,代码没有什么大变化,需要改变的是,在递归的for循环里加上if (i > start && candidates[i] == candidates[i - 1]) continue; 这样可以防止res中出现重复项,然后就在递归调用helper里面的参数换成i+1,这样就不会重复使用数组中的数字了。

class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
sort(candidates.begin(), candidates.end());
helper(candidates, res, {}, target, 0);
return res;
}
void helper(vector<int>& candidates, vector<vector<int>>& res, vector<int> path, int target, int start) {
if (target < 0) return;
if (target == 0) {
res.push_back(path);
}
for (int i = start; i < candidates.size(); i++) {
if (i > start && candidates[i] == candidates[i - 1])
continue;
path.push_back(candidates[i]);
helper(candidates, res, path, target - candidates[i], i + 1);
path.pop_back();
}
}
};

参考资料:

http://www.cnblogs.com/grandyang/p/4419386.html

日期

2018 年 2 月 21 日
2018 年 12 月 19 日 —— 感冒了,好难受

【LeetCode】40. Combination Sum II 解题报告(Python & C++)的更多相关文章

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

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

  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 组合之和 II

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

  4. [leetcode]40. Combination Sum II组合之和之二

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

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

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

  6. LeetCode 40. Combination Sum II (组合的和之二)

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

  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 40 Combination Sum II --- java

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

  9. Java [Leetcode 40]Combination Sum II

    题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...

随机推荐

  1. “equals”有值 与 “==”存在 “equals”只是比较值是否相同,值传递,==地址传递,null==a,避免引发空指针异常,STRING是一个对象==null,对象不存在,str.equals("")对象存在但是包含字符‘''

    原文链接:http://www.cnblogs.com/lezhou2014/p/3955536.html "equals" 与 "==" "equa ...

  2. CSS3单行文本两端对齐

    CSS3实现单行文本两端对齐 p { height: 24px; text-align: justify; text-last-align: justify; } p::after { display ...

  3. javaWeb - 2 — ajax、json — 最后附:后台获取前端中的input type = "file"中的信息 — 更新完毕

    1.ajax是什么? 面向百度百科一下就知道了,这里就简单提炼一下 Ajax即Asynchronous Javascript And XML(异步JavaScript和XML).当然其实我们学的应该叫 ...

  4. javaSE高级篇5 — java8新特性详解———更新完毕

    java8新特性 在前面已经见过一些东西了,但是:挖得有坑儿 1.lambda表达式 lambda表达式是jdk1.8引入的全新语法特性 它支持的是:只有单个抽象方法的函数式接口.什么意思? 就是说: ...

  5. 日常Java 2021/11/2

    Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型.将序列化对象写入文件之后,可以从文件中读取出来 ...

  6. 17. yum

    https://www.linuxidc.com/Linux/2015-04/116331.htm

  7. Netty之Channel*

    Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...

  8. 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常

    1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...

  9. 一起手写吧!promise.all

    Promise.all 接收一个 promise 对象的数组作为参数,当这个数组里的所有 promise 对象全部变为resolve或 有 reject 状态出现的时候,它才会去调用 .then 方法 ...

  10. stlink 无法再keil中识别 按下复位键可以识别

    最近遇到一个很是头痛的问题 本来板子是好好的,就是从公司带回的家里 然后再次用stlink烧写程序的时候就出现了问题: 但是查看电脑端,上面是有stlink的 也就是电脑是好的, 我立刻又试了一下家中 ...