作者: 负雪明烛
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. mysql—从字符串中提取数字(类型1)

    select reason,CHAR_LENGTH(reason),mid(reason,5,CHAR_LENGTH(reason)-5)+0 from `table` 解释: CHAR_LENGTH ...

  2. 质量体系建设之路---可视化的MockServer

    一. 背景 福禄网络作为一家数字权益商品及服务提供商,覆盖了我们衣食住行的各种生活场景的权益内容,对接了如支付宝.京东.银行APP各种渠道,如何能够快速的响应渠道需求,提供稳定的接口服务,这就要求我们 ...

  3. Mybatis批量添加、更新小结

    虽然是很基础的东西,不过难免会忘记,所以写个笔记巩固一下,顺便分享. 实体类: @Data public class EventOrder { ​ private Long id; ​ private ...

  4. 日常Java 2021/10/17

    今天开始Javaweb编译环境调试,从tomcat容器开始,然后mysql的下载,连接工具datagrip,navicat for mysql,然后就是编写自己的sql,安装jdbc,eclipse连 ...

  5. day06 模板层

    day06 模板层 今日内容 常用语法 模板语法传值 模板语法之过滤器 模板语法之标签 自定义过滤器.标签.inclusion_tag(BBS作业用一次) 模板的继承(django前后端结合 那么使用 ...

  6. 【排序算法】——冒泡排序、选择排序、插入排序、Shell排序等排序原理及Java实现

    排序 1.定义: 所谓排序,即是整理文件中的内容,使其按照关键字递增或递减的顺序进行排列. 输入:n个记录,n1,n2--,其对应1的关键字为k1,k2-- 输出:n(i1),n(i2)--,使得k( ...

  7. Android 基础UI组件(二)

    1.Spinner 提供一个快速的方法来从一组值中选择一个值.在默认状态Spinner显示当前选择的值.触摸Spinner与所有其他可用值显示一个下拉菜单,可以选择一个新的值. /** * 写死内容: ...

  8. Xcode功能快捷键

    隐藏xcode command+h退出xcode command+q关闭窗口 command+w关闭所有窗口 command+option+w关闭当前项目 command+control+w关闭当前文 ...

  9. 3.0 rust 项目路径

    $ rustc --versionrustc 1.44.0 (49cae5576 2020-06-01) 将代码存在到不同的文件 main.rs mod aa; fn main() { println ...

  10. NoSQL之Redis学习笔记

    一.NoSQL与Redis 1.什么是NoSQL? NoSQL=Not Only SQL ,泛指非关系型数据库.随着互联网的兴起,传统的关系型数据库已经暴露了很多问题,NoSQL数据库的产生就是为了解 ...