Title:

https://leetcode.com/problems/combination-sum/

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

The same repeated number may be chosen from C unlimited number of times.

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 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3]

思路:基本是DFS的思想。将数组排序之后,每次对当前的这个元素与target比较,看最多能塞进去几个当前的这个元素。

如果数组有相同元素,可以采用注释掉的语句进行去重,或者在递归函数中去重。不去重也不会影响结果。(去掉蓝色的也没有影响,不过蓝色的是为了去重)

注意结束条件: 一般都是index == size(),但这边应该是0 == target

for (int i = (target / candidates[idx]); i >= 0; i--) {
record.push_back(candidates[idx]);
}
用来计算最多压入几个当前元素。
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1,candidates[idx]);
//record.pop_back();
}
弹出栈,再进入递归函数。注意,有可能这个元素就不要压入,所以是i >=0
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
/*vector<int>::iterator pos = unique(candidates.begin(), candidates.end());
candidates.erase(pos, candidates.end());*/
vector<vector<int> > ans;
vector<int> record;
searchAns(ans, record, candidates, target, 0,-1);
return ans;
} private:
void searchAns(vector<vector<int> > &ans, vector<int> &record, vector<int> &candidates, int target, int idx, int preValue) {
if (target == 0) {
ans.push_back(record);
return;
}
if ( idx == candidates.size() || candidates[idx] > target || preValue == candidates[idx]) {
return;
}
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.push_back(candidates[idx]);
}
for (int i = (target / candidates[idx]); i >= 0; i--) {
record.pop_back();
searchAns(ans, record, candidates, target - i * candidates[idx], idx + 1,candidates[idx]);
//record.pop_back();
}
}
};

Title:

https://leetcode.com/problems/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]

思路:DFS搜索。与Combination Sum中的不同,每次搜索的备选项是从当前index开始到数组结束的元素。不包括重复元素。

class Solution {
public:
vector<vector<int> > results;
vector<vector<int> > combinationSum2(vector<int> &num, int target) { if (num.empty() || num.size() == 0)
return results;
sort(num.begin(),num.end());
vector<int> result;
combine(num,0,target,result);
return results;
} void combine(vector<int> &num,int startIndex, int target,vector<int> &result){
if (0 == target){
//cout<<"add"<<endl;
results.push_back(result);
return ;
}
if (0 > target)
return ;
for (int i = startIndex; i < num.size(); i++){
if (i > startIndex && num[i] == num[i-1])
continue;
result.push_back(num[i]);
combine(num,i+1,target-num[i],result);
result.pop_back();
}
}
};

Title:

https://leetcode.com/problems/combination-sum-iii/

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int> > results;
if ( k < || n < )
return results;
vector<int> result;
DFS(results,result,,k,n,);
return results;
}
void DFS(vector<vector<int> >& results, vector<int>& result, int index, int k, int target, int preVal){
if (index == k && target == ){
results.push_back(result);
return ;
}
if (index == k || target <= preVal)
return ;
for (int i = preVal+; i < ; i++){
result.push_back(i);
DFS(results,result,index+,k,target-i,i);
result.pop_back();
}
}
};

LeetCode: Combination Sum I && II && III的更多相关文章

  1. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  2. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  3. combination sum(I, II, III, IV)

    II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...

  4. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

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

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

  6. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  7. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. 子集系列(二) 满足特定要求的子集,例 [LeetCode] Combination, Combination Sum I, II

    引言 既上一篇 子集系列(一) 后,这里我们接着讨论带有附加条件的子集求解方法. 这类题目也是求子集,只不过不是返回所有的自己,而往往是要求返回满足一定要求的子集. 解这种类型的题目,其思路可以在上一 ...

  9. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 【BZOJ】【1934】【SHOI 2007】Vote 善意的投票

    网络流/最小割 简单题= =直接利用最小割的性质: 割掉这些边后,将所有点分成了两部分(两个连通块),我们可以假定与S相连的是投赞成票,与T相连的是投反对票. 那么如果一个小朋友原本意愿是睡觉,那么连 ...

  2. 初尝backbone

    backbone的基础知识在此将不再进行介绍.自己后续应该会整理出来,不过今天先把这几天学的成果用一个demo进行展示. 后续可运行demo将会在sinaapp上分享,不过近期在整理sinaapp上d ...

  3. uLua 学习笔记 之一 lua脚本 打包与读取

    最近要学习热更新,搜了下,选择了ulua这个插件,本人也是新人.对这个插件也是一知半解,不过幸好加了专门讨论这一块的群,这个群的技术氛围还是很浓重的,特别是已经形成了一套自己的lua学习框架.最近周末 ...

  4. 浏览器后退按钮导致jquery动态添加的select option值丢失的解决方法

    监控浏览器返回功能 判断浏览器返回功能 禁用浏览器的后退按钮 JS禁止浏览器后退键 http://volunteer521.iteye.com/blog/830522/ 浏览器返回功能 判断上一页面来 ...

  5. SPOJ LCS 后缀自动机

    用后缀自动机求两个长串的最长公共子串,效果拔群.多样例的时候memset要去掉. 解题思路就是跟CLJ的一模一样啦. #pragma warning(disable:4996) #include< ...

  6. POJ2485Highways

    http://poj.org/problem?id=2485 题意 : 这道题和1258很像,但是这道题求的是最小生成树中最大的那条边,所以在函数里处理一下就行了. 思路 : 赤裸裸的最小生成树啊,但 ...

  7. NET 使用HtmlAgilityPack抓取网页数据

    刚刚学习了XPath路径表达式,主要是对XML文档中的节点进行搜索,通过XPath表达式可以对XML文档中的节点位置进行快速定位和访问,html也是也是一种类似于xml的标记语言,但是语法没有那么严谨 ...

  8. cojs 火龙果 解题报告

    昨天晚上做了一发HNOI,感觉有很多新的idea 于是就选了一个出成题目辣 我们考虑暴力 暴力很明显是把这个图A<=D,B<=E的形态搞出来 之后处理相关的询问 这样我们会很容易得到正解: ...

  9. VA对于开发QT是神器

    我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...

  10. 在PowerDesigner中设计概念模型

    原文:在PowerDesigner中设计概念模型 在概念模型中主要有以下几个操作和设置的对象:实体(Entity).实体属性 (Attribute).实体标识(Identifiers).关系(Rela ...