代码:

个人浅薄的认为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. 从内存中直接运行PE程序

    效果是这样的,假设一个PE数据在内存里面了,我们利用下面我讲的技术可以直接建立一个进程并运行这个PE,当然直接在本进程运行在可以,这两钟技术在前些时日我都有实现,今天我只说关于建立进程并运行的,当然, ...

  2. ExtJs中获得当前选中行号(Grid中多选或者是单选)及Grid的反选(取消选中行)

    多选,如何获得每行的行号: function getlineNum(){    var sm=titleGird.getSelectionModel(); // 获得grid的SelectionMod ...

  3. Azure CLI的版本问题

    Azure支持多种管理方法.命令行方法有: PowerShell,PowerShell只能运行在Windows上 Azure CLI,而Azure CLI可以运行在Windows.MAC以及Linux ...

  4. [转载]rmmod: can't change directory to '/lib/modules': No such file or directory

    转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...

  5. Django基础(二)—— models

    六:Models示例 Django本身提供了非常强大易使用的ORM组件,并且支持多种数据库. 配置连接数据文件 在自己创建的project 目录下编辑settings.py DATABASES = { ...

  6. AngularJS:SQL

    ylbtech-AngularJS:SQL 1.返回顶部 1. AngularJS SQL 在前面章节中的代码也可以用于读取数据库中的数据. 使用 PHP 从 MySQL 中获取数据 AngularJ ...

  7. Shell脚本把文件从GBK转为UTF-8编码

    http://www.jb51.net/article/51308.htm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  8. MFC简单的橡皮筋程序

    void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point) { //以下三个是在CMainWindow中定义 m_ptFrom=point; m ...

  9. Oracle中REGEXP_SUBSTR函数(字符串转多行)

    Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20',' ...

  10. 10-13C#语句(1)

    C#语句:判断.循环.形成程序的分支和循环 1.语句分类 1)顺序语句 2)分支语句 3)循环语句 2.语句 选择控制:if,else,switch,case 循环控制:while,do,for,fo ...