【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II
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]
解法一:
最直接的想法就是把所有子集都列出来,然后逐个计算和是否为target
但是考虑到空间复杂度,10个数的num数组就有210个子集,因此必须进行“剪枝”,去掉不可能的子集。
先对num进行排序。
在遍历子集的过程中:
(1)单个元素大于target,则后续元素无需扫描了,直接返回结果。
(2)单个子集元素和大于target,则不用加入当前的子集容器了。
(3)单个子集元素和等于target,加入结果数组。
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int> > result;
vector<vector<int> > subsets;
vector<int> empty;
subsets.push_back(empty);
sort(num.begin(), num.end());
for(int i = ; i < num.size();)
{//for each number
int count = ;
int cur = num[i];
if(cur > target) //end
return result;
while(i < num.size() && num[i] == cur)
{//repeat count
i ++;
count ++;
}
int size = subsets.size(); //orinigal size instead of calling size() function
for(int j = ; j < size; j ++)
{
vector<int> sub = subsets[j];
int tempCount = count;
while(tempCount --)
{
sub.push_back(cur);
int sum = accumulate(sub.begin(), sub.end(), );
if(sum == target)
{
result.push_back(sub);
subsets.push_back(sub);
}
else if(sum < target)
subsets.push_back(sub);
}
}
}
return result;
}
};

解法二:递归回溯
需要注意的是:
1、在同一层递归树中,如果某元素已经处理并进入下一层递归,那么与该元素相同的值就应该跳过。否则将出现重复。
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3
那么第二个1就应该跳过,因为后续所有情况都已经被覆盖掉。
2、相同元素第一个进入下一层递归,而不是任意一个
例如:1,1,2,3
如果第一个1已经处理并进入下一层递归1,2,3,那么两个1是可以同时成为可行解的
而如果选择的是第二个1并进入下一层递归2,3,那么不会出现两个1的解了。
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
sort(num.begin(), num.end());
vector<vector<int> > ret;
vector<int> cur;
Helper(ret, cur, num, target, );
return ret;
}
void Helper(vector<vector<int> > &ret, vector<int> cur, vector<int> &num, int target, int position)
{
if(target == )
ret.push_back(cur);
else
{
for(int i = position; i < num.size() && num[i] <= target; i ++)
{
if(i != position && num[i] == num[i-])
continue;
cur.push_back(num[i]);
Helper(ret, cur, num, target-num[i], i+);
cur.pop_back();
}
}
}
};

【LeetCode】40. Combination Sum II (2 solutions)的更多相关文章
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
随机推荐
- 5.synchronized锁重入
package demo1; /** * synchronized锁重入 * Created by liudan on 2017/6/5. */ public class MyThread5_sync ...
- C# 中奇妙的函数–8. String Remove() 和 Replace()
http://www.cnblogs.com/multiplesoftware/archive/2011/09/27/2192710.html 当对字符串进行操作时,我们经常要删除或者是替换一部分子字 ...
- easyui加入自己定义图标
近期用easyui发现图标挺少的,事实上能够另外加入一个css样式,只是我偷懒,直接在easyui的css里面加入了. 以下是文件夹: icon.css是easyui的默认样式文件.ext_icons ...
- 自定义cas客户端核心过滤器AuthenticationFilter
关于cas客户端的基本配置这里就不多说了,不清楚的可以参考上一篇博文:配置简单cas客户端.这里是关于cas客户端实现动态配置认证需要开发说明. 往往业务系统中有些模块或功能是可以不需要登录就可以访问 ...
- Python编程-Office操作-操作Excel(中)
例子文件如下: 一些复杂的读取操作getCells.py import openpyxl wb = openpyxl.load_workbook('example.xlsx') sheet = wb. ...
- linux下apache的使用
Linux安装配置apache http://www.cnblogs.com/fly1988happy/archive/2011/12/14/2288064.html 1.获取软件: http://h ...
- 【树莓派】制作树莓派所使用的img镜像(一)
最近一直在折腾树莓派,前几天装了10台设备,最近又来了15台开发板子.基本每台设备都需要进行如下操作: 1.安装树莓派OS,并配置键盘.时区.语言编码格式等: 2.新增组.用户.配置静态IP地址: 3 ...
- php之快速入门学习-3(print和echo)
PHP echo 和 print 语句 echo 和 print 区别: echo - 可以输出一个或多个字符串 print - 只允许输出一个字符串,返回值总为 1 提示:echo 输出的速度比 p ...
- Spring boot 与quart集成并在Job中注入服务
1:AutowiringSpringBeanJobFactory.java package com.microwisdom.grgzpt.jobs; import org.quartz.spi.Tri ...
- IntelliJ IDEA 注册码失效
破解补丁无需使用注册码,下载地址:http://idea.lanyus.com/jar/JetbrainsCrack-2.6.2.jar idea14 keygen下载地址:http://idea.l ...