代码:

个人浅薄的认为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. uoj#87. mx的仙人掌

    //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #define Rep(i, ...

  2. Poj 3287 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. linux find 实例 ***

    使用选项:find [路径] <表达式> [操作] 1.name选项,按名称查找查找当前目录下的manage.py 文件:find . -name ‘manage.py’ 2.atime/ ...

  4. extjs控制器调用其他视图的函数实现控件赋值。

  5. 蓝桥杯 历届试题 PREV-1 核桃的数量

    历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑 ...

  6. Json-lib 进行java与json字符串转换之二

    二.list和json字符串的互转 list-->>json字符串 public static void listToJSON(){ Student stu=new Student(); ...

  7. SqlServer——判断对象是否存在

    对以下对象判断是否存在:database.table.proc.触发器.临时表.索引.对于这些对象的判断是通过数据表 SysObjects来获得的. 一.基础知识 1.SysObjects系统表 对于 ...

  8. Linux-CentOS 学习的坎坷路 (一) 网络配置篇

    自己学习的地址:http://www.imooc.com/view/175 学到2.8章节,配置IP这一块,妈蛋,他直接跳过了,都不知道怎么配置,无奈,只能Search 先是找到配置IP的方法: ht ...

  9. cookie禁用后重定向跳转时session的跟踪

  10. ssh整合(dao使用hibernateTemplate)