Combination Sum

Given a set of candidate numbers (C) (without duplicates) 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.
  • 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的思想来解题。

数组从头到尾,每次决定是否将当前的值加入组合:

  • 如果是,那么判断当前组合是否sum == target,

    • 等于——>将该组合加入解集合,return;
    • 不等于——>继续递归
  • 如果不是,那么index+1(数组向后移动),继续递归

代码非常容易理解,如下所示:

class Solution {
public:
vector<vector<int>> re;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<int> temp;
search(candidates,temp,0,candidates.size(),target,0);
return re;
}
void search(vector<int> candidates,vector<int> temp,int index,int len,int target,int cur){ if(index >= len || cur > target) return; //do not select this value,search deepper,index need increase
search(candidates,temp,index+1,len,target,cur); /*
* select this value,and judge if it equals the target.
* If yes,push back the vector and return,if not,search continue
* index don't change,because the number can be repeated
*/
temp.push_back(candidates[index]);
cur += candidates[index];
if(cur == target){
re.push_back(temp);
return;
}
else{
search(candidates,temp,index,len,target,cur);
}
}
};

[LeetCode]Combination Sum题解(DFS)的更多相关文章

  1. LeetCode Combination Sum III

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

  2. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  3. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  4. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

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

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

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

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

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

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

  8. [LeetCode] Combination Sum 组合之和

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

  9. LeetCode: Combination Sum II 解题报告

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

随机推荐

  1. 使用unordered_map提升查找效率

    在对网络数据包流(Flow)进行处理的时候,一开始为了简单使用了vector做为Flow信息的存储容器,当其中的元素达到几十万时,程序的执行速度让人无法忍受.已经对vector进行过合理的预先rese ...

  2. K:java中的安全模型(沙箱机制)

    本博文整合自:Java安全--理解Java沙箱.Java 安全模型介绍.Java的沙箱机制原理入门 相关介绍:  我们都知道,程序员编写一个Java程序,默认的情况下可以访问该机器的任意资源,比如读取 ...

  3. Oracle递归查询(start with)

    写代码时碰到要弄清楚Oracle的role之间的传递关系,就是有role A的话,可以通过grant A to B,把A赋予给B,又通过grant B to C .那我想知道所有role中,有哪些ro ...

  4. java_I/O字符流

    I/O流(Stream) INPUT:输入流,从文件里读OUPUT:输出流,写内容到文件 IO流分为:字符流和字节流 字符流:处理纯文本文件. 字节流:处理可以所有文件. 字符输出流测试: @Test ...

  5. 考试题T3

    题意分析 这题一看没有什么思路 幸好我们机房的红太阳\(ghj1222\)切了这道题 首先我们考虑风跑一个来回之后人怎么样 就是跑了一个区间 也就是风跑了若干个来回之后 人跑了若干个区间 所以我们考虑 ...

  6. Laravel - 从百草园到三味书屋 "From Apprentice To Artisan"目录

    Laravel - 从百草园到三味书屋 "From Apprentice To Artisan"目录 https://my.oschina.net/zgldh/blog/38924 ...

  7. Java多线程——对象组合

    我们不希望对每一次的内存访问都进行分析以确保程序是线程安全的,而是希望将一些现有的线程安全组件组合为更大规模的组件或者程序,这里介绍一些组合模式,这些组合模式能够使一个类更容易成为线程安全的,并且在维 ...

  8. Luogu P1951 收费站_NOI导刊2009提高(2)

    二分答案+堆优Dijkstra 这个题有些巧妙. 首先,因为要在油量耗完之前跑到终点,所以我们可以用最短路.只要从\(s\)出发到\(t\),它的最短距离大于油量,我们就可以断定它一定走不通,直接输出 ...

  9. css左右布局,左侧固定,右侧自适应

    实现布局的几种方法,见代码: <!DOCTYPE html> <html lang="cn"> <head> <meta charset= ...

  10. HLS:OpenCV和RTL代码转换关系

    OpenCV 图像处理是基于存储器帧缓存而构建的, 它总是假设视频帧数据存放在外部 DDR 存储器中. 由于处理器的小容量高速缓存性能的限制, 因此, OpenCV 访问局部图像性能较差. 并且, 从 ...