美文网首页
dfs-lc17&bfs-116

dfs-lc17&bfs-116

作者: 锦绣拾年 | 来源:发表于2021-02-09 11:04 被阅读0次

    [TOC]

    图的三种算法伪代码:

    dfs: 会枚举完所有完整的路径。【很多递归问题、子集问题、枚举问题等】

    void dfs(G){
        time = time+1//发现时间
        for(G的每一个顶点u){
            if(vis[u]==false){
                vis[u]=true;
                dfs(u);
               
            }
        }     
       time = time+1   //完成时间,完成时间倒序排序是拓扑排序
    }
    void bfs(){
        queue q;
        while(q非空){
            取出q的队首元素u进行访问
            for(从u出发可达的所有顶点v){
                if(v没被访问过)
                    加入队列
            }
        }
    }
    //循环n次
    void Dijkstra(G,d[],s){
        for(循环n次){
            u=使d[u]最小还未被访问的顶点;
            记u已被访问;
            for(从u出发能到达所有顶点v){
                if(v未被访问&&以u为中介点能使s到顶点v距离更优){
                    优化d[v]
                }
            }
        }
        
    }
    
    

    之前的一些总结
    https://www.jianshu.com/p/04d31889c4bf

    https://www.jianshu.com/p/65a10c2767b9

    https://www.jianshu.com/p/411ee5c6dc7c

    dfs-lc17

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

    给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解题思路:

    dfs,

    执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户

    内存消耗:6.4 MB, 在所有 C++ 提交中击败了95.33%的用户

    class Solution {
    public:
    
        void dfs(vector<string>& res,string s,int index,string t,map<char,string>& phone){
            if(index == s.length()){
                if(t!="")
                    res.push_back(t);
                return;
            }
            string tmp = phone[s[index]];
            for(int i=0;i<tmp.length();i++){
                dfs(res,s,index+1,t+tmp[i],phone);
            }
        }
        vector<string> letterCombinations(string digits) {
            map<char,string> phone;
            phone['2']="abc";
            phone['3']="def";
            phone['4']="ghi";
            phone['5']="jkl";
            phone['6']="mno";
            phone['7']="pqrs";
            phone['8']="tuv";
            phone['9']="wxyz";
            vector<string> res;
            
            dfs(res,digits,0,"",phone);
            
            
            return res;
    
        }
    };
    

    注意可以这么写:

     map<char,string> mp={{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},  {'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};
    

    bfs-lc116

    在bfs进行层次遍历时,很多时候需要记录层数。

    116. 填充每个节点的下一个右侧节点指针

    好像蛮顺利的。bfs:

    给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
    
    struct Node {
      int val;
      Node *left;
      Node *right;
      Node *next;
    }
    填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
    
    初始状态下,所有 next 指针都被设置为 NULL。
    
     
    
    进阶:
    
    你只能使用常量级额外空间。
    使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
     
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    输入:root = [1,2,3,4,5,6,7]
    输出:[1,#,2,3,#,4,5,6,7,#]
    解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
    
    

    进阶使用常量级额外空间还未实现。单纯使用bfs遍历。

    执行用时:28 ms, 在所有 C++ 提交中击败了56.89%的用户

    内存消耗:16.9 MB, 在所有 C++ 提交中击败了38.24%的用户

    /*
    // Definition for a Node.
    class Node {
    public:
        int val;
        Node* left;
        Node* right;
        Node* next;
    
        Node() : val(0), left(NULL), right(NULL), next(NULL) {}
    
        Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
    
        Node(int _val, Node* _left, Node* _right, Node* _next)
            : val(_val), left(_left), right(_right), next(_next) {}
    };
    */
    //bfs 并记录层
    class Solution {
    public:
        Node* connect(Node* root) {
            // Node* res;
            if(!root)
                return root;
            queue<Node*> pros;
            pros.push(root);
            queue<int> ceng;
            ceng.push(0);
            while(!pros.empty()){
                int index=ceng.front();
                ceng.pop();
                Node* nownode = pros.front();
                pros.pop();
                if(nownode->left){
                    pros.push(nownode->left);
                    ceng.push(index+1);
                }
                if(nownode->right){
                    pros.push(nownode->right);
                    ceng.push(index+1);
                }
    
                if(ceng.empty()||ceng.front()!=index){
                    nownode->next=NULL;
                }else{
                    nownode->next=pros.front();
                }
    
    
            }
            return root;
            
        }
    };
    

    相关文章

      网友评论

          本文标题:dfs-lc17&bfs-116

          本文链接:https://www.haomeiwen.com/subject/gjgqxltx.html