回溯法和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 ...
随机推荐
- 【java规则引擎】模拟rete算法的网络节点以及匹配过程
转载请注明:http://www.cnblogs.com/shangxiaofei/p/6340655.html 本文只用于理解rete算法,通过一个规则的编译成的网络结构,以及匹配过程去理解rete ...
- uboot中的TEXT_BASE
转载:http://blog.csdn.net/xxblinux/article/details/6281295 我们都知道U-BOOT分为两个阶段,第一阶段是(~/cpu/arm920t/start ...
- 嵌入式系统LINUX环境搭建
Linux kernel Complier: http://supportopensource.iteye.com/blog/680483 sudo make mrproper 净化解 ...
- java流2
总结:字符转换 package com.b; //流类 import java.io.*; public class uy { // 读取字符 public static void main(Stri ...
- SpringBoot自动化配置之三:深入SpringBoot:自定义EnableAutoConfiguration
前言 上面几篇文章介绍了SpringFramework的一些原理,这里开始介绍一下SpringBoot,并通过自定义一些功能来介绍SpringBoot的原理.SpringBoot在SpringFram ...
- pom.xm首行报错Failure to transfer org.codehaus.plexus:plexus-components:pom:1.1.20
从SVN导出一个Maven项目,pom.xml首行报错: Failure to transfer org.codehaus.plexus:plexus-components:pom:1.1.20 fr ...
- 利用DNS进行传输数据
曾经有这样一道题目,困了我数个小时,最后我尝试利用此方法时我知道真相的时候,眼泪已掉下来. 遇到的是一个流量分析题,分析DNS数据,拿到flag 流量如图所示: 进入linux,提取: [root@s ...
- [转]Missing MSS Settings in Security Options of Group Policy (GPO)
I'm currently working on a new Windows Server 2012 and Windows 8 project. As part of that project is ...
- 重新认识synchronized(上)
synchronized在JDK5之前一直被称为重量级锁,是一个较为鸡肋的设计,而在JDK6对synchronized内在机制进行了大量显著的优化,加入了CAS,轻量级锁和偏向锁的功能,性能上已经跟R ...
- 2015.1.15 利用航线id取所有点的函数创建视图
1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...