代码:

个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解。而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法。

下面这个函数就是我的DFS的函数,先介绍一下参数的含义,index表示当前要判断是否合适的candidates中的元素的下标,t表示即将要把新数据加入的位置。

 void backTrace(int sum, int target, int a[], int index, int t, vector<int>& candidates)
{
if (sum == target)
{
vector<int> b;
for (int i = ; i < t; i++)
{
b.push_back(a[i]);
}
sort(b.begin(),b.end());
result.push_back(b);
}
if (sum>target)
{
return;
}
for (int i = index; i < candidates.size(); i++)
{
a[t] = candidates[i];
backTrace(sum + candidates[i], target, a, i, t + , candidates);
}
}

这是很典型的深搜的题目了,我写回溯法特别容易出错,一个好的解决方法就是画出简易的、局部的解法树,然后根据解法树判断什么时候回溯,回溯的下一步是什么,回溯的逻辑关系是循环控制还是有其他方式控制(二叉树就是简单的左右控制),还有就是当前参数就是当前的数据源不能混。

哈哈哈哈!!!

这个题我也有改进技巧啦:

 #include<iostream>
#include<vector>
#include<algorithm> using namespace std; vector<vector<int>> result;
int a[]; void backTrace(int sum, int target, int a[], int index, int t, vector<int>& candidates)
{
if (sum == target)
{
vector<int> b;
for (int i = ; i < t; i++)
{
b.push_back(a[i]);
}
result.push_back(b);
}
if (sum>target)
{
return;
}
for (int i = index; i < candidates.size(); i++)
{
a[t] = candidates[i];
backTrace(sum + candidates[i], target, a, i, t + , candidates);
if (candidates[i] + sum > target)
return;
}
} vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
backTrace(, target, a, , , candidates);
return result;
} int main()
{
vector<int> can = { , , , };
combinationSum(can, );
for (int i = ; i < result.size(); i++)
{
for (int j = ; j < result[i].size(); j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
}

改进点就是先对candidates进行从小到大的排序,然后就可以加上30~31的这行代码了,这个能减少不少无用的尝试,然后就是结果集,由于我们已经排好序了,且加入是从小到大所以,后来的就不需要排序了,直接添加就好了。少了第10行。

哈哈哈哈哈哈。。。。

我的一个小伙伴提供了一个思路,根据这个思路可以不用recursion,下面介绍一下,明天叫上代码:

先用target去减集合中的第一个元素然后在集合中寻找减的结果,如果有则作为一个成功的探索,如果没有继续减该元素然后继续寻找,直到减的结果小于零。再去尝试集合中的下一个元素。

回溯法和DFS leetcode Combination Sum的更多相关文章

  1. [LeetCode]Combination Sum题解(DFS)

    Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), f ...

  2. [Leetcode] Combination Sum 系列

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

  3. LeetCode Combination Sum III

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

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

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

  5. LeetCode: Combination Sum 解题报告

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

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

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

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

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

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

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

  9. [LeetCode] Combination Sum 组合之和

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

随机推荐

  1. Redis底层探秘(五):Redis对象

    前面几篇文章,我们一起学习了redis用到的所有主要数据结构,比如简单动态字符串(sds).双端链表.字典.压缩列表.整数集合等等. redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这 ...

  2. Python函数-eval()

    eval(source[, globals[, locals]]) 作用: 将字符串str当成有效的表达式来求值并返回计算结果.参数:source:一个Python表达式或函数compile()返回的 ...

  3. Linux查找/扫描局域网打印机IP

    假设在 192.168.10.* 有一台网络打印机,但是我们不知道它的地址.一种笨方法就是在浏览器中依次输入 192.168.10.1 到 192.168.10.254,看是否出现管理页面. 另一种思 ...

  4. BZOJ4170:极光

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  5. Python 列表的切片和连接

    一.定义一个list >>> a = [1, 3, 4, 5, 'a', 's'] >>> a [1, 3, 4, 5, 'a', 's'] 二.获取列表中前3个元 ...

  6. vlan之间Hybrid端口配置

    要求:1.PC1和PC2不能互相访问,但是都能访问PC3 SW1配置:vlan batch 10 20 100 interface Ethernet0/0/1                     ...

  7. Process使用

    最近在一个项目中,需要在C#中调用cmd窗口来执行一个命令,使用到了Process这个类,使用过程中遇到不少问题,好在终于解决了.赶紧记录下来. Process process = new Proce ...

  8. 数据格式化和ModelAttribute注解的介绍

    关于数据传递: 客户端传递数据到服务端: 1.使用普通的形式 A.传递简单的数据 如果是说你传递的数据的名称跟控制层中的形参的名称不一致的情况下需要使用 注解: @RequestParam()如果存在 ...

  9. 查看osdmap命令

    标签(空格分隔): ceph,ceph运维,osdmap 方法一: 最直接,简单的命令: [root@node3 ~]# ceph osd tree ID CLASS WEIGHT TYPE NAME ...

  10. 网络监控之一:ss(Socket Statistics)

    ss是Socket Statistics的缩写. 顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的 ...