DFS template and summary

最近一直在学习Deep Frist Search,也在leetcode上解了不少的题目。从最开始的懵懂,到现在基本上遇到一个问题有了思路。我现在还清晰的接的今年2月份我刚开始刷提的时候做subsets时那个吃力的劲,脑子就是转不过来到底该如何的递归。

void DFS_no_return(vector<TYPE>& solution_set, TYPE& solution, int idx){
    // DFS terminate condition
    // Normally there are two major categories
    // One is for some number, already reach this depth according to the question, stop
    // get to the end of an array, stop
    if(idx == n || idx == (vector/string).size()){
        solution_set.push_back(solution);
        return;
    }
    // very seldom only when our program met some efficiency problem
    // do prune to reduce the workload
    // no need do DFS if we already get the value of current element, just return
    if(hash_table[input] != hash_table.end()){
        return;
    }

    for(int i = 0; i < n; i++){
        if(visited[i] == 0){
            visited[i] = 1;
            DFS_no_return(solution_set, solution, idx + 1);
            visited[i] = 0;
        }
    }
    return;
}

对于有返回值的DFS来说。

TYPE DFS_with_return(int idx){
    // DFS terminate condition
    // Normally there are two major categories
    // One is for some number, already reach this depth according to the question, stop
    // get to the end of an array, stop
    if(idx == n || idx == (vector/string).size()){
        return 1/0/"";
    }
    // very seldom only when our program met some efficiency problem
    // do prune to reduce the workload
    // no need do DFS if we already get the value of current element, just return
    if(hash_table[input] != hash_table.end()){
        return hash_table[input];
    }

    TYPE ans;
    for(int i = 0; i < n; i++){
        if(visited[i] == 0){
            // normally, here should to add/concatenate the value in current level with the value returned from lower level
            ans += DFS_no_return(idx + 1);
        }
    }
    // if we need prune
    // keep current value here
    hash_table[input] = ans;
    return ans;
}

总结:

时间: 2024-10-15 19:09:28

DFS template and summary的相关文章

Binary Search Tree DFS Template

Two methods: 1. Traverse 2. Divide & Conquer 1 // Traverse: usually do not have return value 2 public class Solution { 3 public void traverse(TreeNode root) { 4 if (root == null) 5 return; 6 traverse(root.left); 7 traverse(root.right); 8 } 9 } 10 11

Design Pattern - Template Method(C )

Definition Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Participants The classes and/or obj

设计模式系列13:模板方法模式(Template Method Pattern)

定义 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤.    --<设计模式GoF> UML类图 使用场景 有多个子类共有的方法,且逻辑相同,可以抽象到父类中作为模板方法实现,避免代码重复. 重要的,复杂的方法,可以考虑作为模板方法. C#代码实现 using System; namespace DoFactory.GangOfFour.Template.Structural { /// <summary>

t4 加载文件到解决方案

Use EnvDTE add/remove multiple files to project By admin | décembre 17, 2013 Un commentaire Project Source Source: EnvDTEManipulateLicence: MIT EnvDTE is an assembly-wrapped COM library containing the objects and members for Visual Studio core automa

ant+jmeter生成html报告

源博文来自于  http://my.oschina.net/hellotest/blog/517518 主要应用于接口的回归或者性能的简单查看功能.操作为先在jmeter中写好测试计划,保存为jmx文件,在cmd窗口对应路径下执行ant命令,会完成jmx计划的执行和生成jtl文件,并将jtl文件转化为html页面进行查看.后续可扩展为接入jenkins集成,并邮件发送. 一 安装ant 下载apache-ant,放到对应路径下,然后进行环境变量配置.系统变量的CLASSPATH添加E:\Inst

416. Partition Equal Subset Sum

Problem statement: Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Each of the array element will not exceed 100. The array

565. Array Nesting

Problem statement: A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. Sets S[K] for 0 <= K < N are defined as follows: S[K] = { A[K], A[A[K]], A[A[A[K]]], ... }. Sets S[K] are

52. N-Queens II

Problem statement: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Solution: Compare with 51. N-Queens, this problem wants the number of  possible solutions. It also belongs

Binary Tree

Binary Tree dfs Traversal -preorder/inorder/postorder -divide&conquer -introduce dfs template Preorder : Inorder: Postorder: Divide & Conquer : -Merge Sort (arrayO(N)空间, sorted list 不需要) 必会!待补充 -Quick Sort 必会!待补充 -Most of the Binary Tree Problem 题