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. Web App之一

    JSP/HTML/CSS---------View(不包含任何的数据,只作为基本的layout) JS------------------------Data(update JSP/HTML)

  2. POJ2411 Mondriaan's Dream 轮廓线dp

    第一道轮廓线dp,因为不会轮廓线dp我们在南京区域赛的时候没有拿到银,可见知识点的欠缺是我薄弱的环节. 题目就是要你用1*2的多米诺骨排填充一个大小n*m(n,m<=11)的棋盘,问填满它有多少 ...

  3. hdu 3336 Count the string(思维可水过,KMP)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

  4. 关联式容器(associative containers)

    关联式容器(associative containers) 根据数据在容器中的排列特性,容器可分为序列式(sequence)和关联式(associative)两种. 标准的STL关联式容器分为set( ...

  5. 深入浅出ES6(七):箭头函数 Arrow Functions

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教 ...

  6. 移动开发之meta篇

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable= ...

  7. 【PHPsocket编程专题(实战篇①)】php-socket通信演示

    建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket ,另一个运行于服务器端,称为ServerSocket . 套接字之间的连接过程分为三个步骤:服务器监听,客户 ...

  8. SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍

    SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...

  9. 250. Count Univalue Subtrees

    题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes ...

  10. JVM学习笔记(二)------Java代码编译和执行的整个过程

    Java代码编译是由Java源码编译器来完成,流程图如下所示: Java字节码的执行是由JVM执行引擎来完成,流程图如下所示: Java代码编译和执行的整个过程包含了以下三个重要的机制: Java源码 ...