回溯法和DFS leetcode Combination Sum
代码:
个人浅薄的认为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的更多相关文章
- [LeetCode]Combination Sum题解(DFS)
Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), f ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- 基于Python语言使用RabbitMQ消息队列(二)
工作队列 在第一节我们写了程序来向命名队列发送和接收消息 .在本节我们会创建一个工作队列(Work Queue)用来在多个工人(worker)中分发时间消耗型任务(time-consuming tas ...
- COGS 2259 异化多肽 —— 生成函数+多项式求逆
题目:http://cogs.pro:8080/cogs/problem/problem.php?pid=2259 如果构造生成函数是许多个 \( (1+x^{k}+x^{2k}+...) \) 相乘 ...
- Azure基于角色的用户接入控制(RBAC)
RBAC是Role Based Access Control是基于角色的接入控制的简称.在Azure推出ARM以后,对Azure各种资源的管理粒度已经非常细致,使得RBAC成为可能. 通过RBAC可以 ...
- ARM模式下创建Express Route
在Azure的ARM模式下,创建Express Route的命令和ASM模式下是有一些区别的. 本文将介绍在ARM模式下,如果创建Express Route的Circuit. 1. 查看支持的Serv ...
- (转)更改Web.config中对上传文件大小限制
.net上传超过200K的图片的时候,会跳转到404,但是url没有错误,真J8的坑啊. 本文转载自:http://www.cnblogs.com/zwffff/archive/2009/04/29/ ...
- xargs 命令使用小记
ls -1|xargs -t -i mv {} noncredit{} 注意: ls -1 是123的1,不是lmn的l
- python第十一天-----补:缓存操作
memcached,首先下载python-memcached模块,在cmd中执行pip install python-memcached即可 memcached比较简单,默认情况仅支持简单的kv存储, ...
- Mongodb 3.6 副本集测试及添加删除节点等操作
下载tar包并安装curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.8.tgz [root@mysqlt ...
- kubernetes 学习 ingress
ingress是Kubernetes 暴露服务的一种方式. Ingress由两部分组成:Ingress Controller 和 Ingress 服务. Ingress Contronler ...
- 人脸识别FaceNet+TensorFlow
一.本文目标 利用facenet源码实现从摄像头读取视频,实时检测并识别视频中的人脸.换句话说:把facenet源码中contributed目录下的real_time_face_recognition ...