【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛
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:
- All numbers (including target) will be positive integers.
- 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++)的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
随机推荐
- 每日自动健康打卡(Python+腾讯云服务器)
每日自动健康打卡(Python+腾讯云服务器) 1.配置需要 python3.7,Chrome或者Edeg浏览器,Chrome驱动或者Edge驱动 #需要配置selenium库,baidu-aip库, ...
- R语言学习记录(一)
(R基础) 对象:什么是对象呢,其实就是一个名称而已,在R中存储的数据 就是一个R对象 a <- 1 ###其中'<-'表示的是一个赋值符号 这句话表示的是,将1赋值给a b <- ...
- Azkaban(二)【WorkFlow案例实操】
目录 1.使用步骤 2.案例: 1.hello word 2.作业依赖[dependsOn配置作业的依赖关系] 3.内嵌工作流 4.全局配置 [在开头通过config进行配置,后续可以通过${属性名} ...
- 理解各种不同含义的 new 和 delete
new operator new操作符 operator new 操作符new placement new 定位new string *ps = new string("Memory Man ...
- GO 总章
GO 学习资源 go 代理 GO 语言结构 GO 数字运算 GO 时间处理 GO 定时器 GO 异常处理 go recover让崩溃的程序继续执行 GO Exit Fatal panic GO 通过进 ...
- linux如何安装缺失依赖
这里要提到一个网站https://pkgs.org/,他是linux系统的一个相关网站,里面都是相关内容 Warning: RPMDB altered outside of yum. ** Found ...
- vue2 页面路由
vue官方文档 src/views/Login.vue <template> <div> <h2>登录页</h2> </div> </ ...
- shell awk命令字符串拼接
本节内容:awk命令实现字符串的拼接 输入文件的内容: TMALL_INVENTORY_30_GROUP my163149.cm6 3506 5683506 mysql-bin.000013 3273 ...
- 【C/C++】函数的默认参数/函数的占位参数/函数重载/注意事项
函数的默认参数 返回值类型 函数名(参数=默认值){} #include <iostream> using namespace std; int func(int a = 10, int ...
- 纯css设置元素高度与宽度相等
设置图片高度等于宽度 .img-box{ width:100%; height:0; position: relative; padding-bottom: 100% } .img-box img{ ...