Leetcode40. Combination Sum组合总数2 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
- 所有数字(包括目标数)都是正整数。
- 解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5, 所求解集为: [ [1,2,2], [5] ]
相比组合种数这道题加了个新加了去重操作,即递归了一个数后,回溯到此层时,把数组之后和该数相同的给过滤掉,防止出现重复的答案。
bool cmp1(int x, int y)
{
return x < y;
}
class Solution {
public:
vector<vector<int> > res;
vector<vector<int>> combinationSum2(vector<int>& candidates, int target)
{
int len = candidates.size();
sort(candidates.begin(), candidates.end(), cmp1);
vector<int> temp;
DFS(candidates, target, temp, 0);
return res;
}
void DFS(vector<int> candidates, int val, vector<int> &v, int cnt)
{
if(val == 0)
{
res.push_back(v);
return;
}
for(int i = cnt; i < candidates.size(); i++)
{
if(candidates[i] <= val)
{
v.push_back(candidates[i]);
DFS(candidates, val - candidates[i], v, i + 1);
v.pop_back();
for(; i < candidates.size() - 1; i++)
{
if(candidates[i] == candidates[i + 1])
continue;
else
break;
}
}
}
}
};
Leetcode40. Combination Sum组合总数2 II的更多相关文章
- LeetCode40 Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 039 Combination Sum 组合总和
给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明: 所有数字(包括目标)都是正整数. 解集合 ...
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- Leetcode39.Combination Sum组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- Git从fork分支开始的过程整理
文章适用于团队合作的时候多个人向一个repo贡献,整理了Git从fork分支开始的过程. 1. Fork 在github上你要贡献的repo(eg.http://github/remote/test. ...
- java最大余数法(百分比算法Echarts)
最近工作中使用Echarts开发报表的时候遇到了这样的一个问题,需求是一个div中左边是一个环形图表,右边是一个表格,表格中展示图表中每个类别占用的百分比.存在的问题:1.当存在四舍五入的时候,Ech ...
- google移动版针对智能手机、非智能手机的蜘蛛的User-agent
非智能手机蜘蛛的User-agent有以下两个 SAMSUNG-SGH-E250/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 UP.Browser/6.2. ...
- 2019-5-24-WPF-源代码-从零开始写一个-UI-框架
title author date CreateTime categories WPF 源代码 从零开始写一个 UI 框架 lindexi 2019-05-24 15:54:36 +0800 2018 ...
- scanf读入有空格字符串
当不支持gets时,getline又比较慢,可以使用scarf("%[^\n]s", str);来读入以换行表示读完的字符串,其中[^char]表示以char为结束.
- Windows API GetShortPathName GetLongPathName
函数原型: The GetShortPathName function retrieves the short path form of a specified input path. DWORD G ...
- Django项目:CRM(客户关系管理系统)--65--55PerfectCRM实现CRM客户报名状态颜色变化
# kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...
- Spring注解驱动开发(一)-----组件注册
注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...
- Intelij Idea 2016破解
在注册时选择License server,输入http://www.iteblog.com/idea/key.php,点击OK
- sql 书写顺序
SELECT select_list [ INTO new_table ] FROM table_source [ WHERE search_condition ] [ GROUP BY group_ ...