zoukankan      html  css  js  c++  java
  • Word Ladder II [leetcode]

    本题有几个注意点:

    1. 回溯找路径时。依据路径的最大长度控制回溯深度

    2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束。不须要遍历下一层了。

    3. 能够将字典中的单词删除。替代visited的set,这样优化以后时间从1700ms+降到800ms+

    代码例如以下:

    class Solution {
    public:
        vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
            set<string> queue[2];
            queue[0].insert(start);
            vector<vector<string>> res;
            bool find = false;
            int length = 1;
            bool cur = false;
            map<string, set<string>> mapping;
            
            //bfs
            while (queue[cur].size() && !find)
            {
                length++;
                for (set<string>::iterator i = queue[cur].begin(); i != queue[cur].end(); i++)//delete from dictionary
                    dict.erase(*i);
                for (set<string>::iterator i = queue[cur].begin(); i != queue[cur].end(); i++)
                {
                    for (int l = 0; l < (*i).size(); l++)
                    {
                        string word = *i;
                        for (char c = 'a'; c <= 'z'; c++)
                        {
                            word[l] = c;
                            if (dict.find(word) != dict.end())
                            {
                                if (mapping.find(word) == mapping.end()) mapping[word] = set<string>();
                                mapping[word].insert(*i);
                                if (word == end) find = true;
                                else             queue[!cur].insert(word);
                            }
                        }
                    }
                }
                queue[cur].clear();
                cur = !cur;
            }
            if (find)
            {
                vector<string> temp;
                temp.push_back(end);
                getRes(mapping, res, temp, start, length);
            }
            
            return res;
        }
        
        void getRes(map<string, set<string>> & mapping, vector<vector<string>> & res, vector<string> temp, string start, int length)
        {
            if (temp[0] == start)
            {
                res.push_back(temp);
                return;
            }
            if (length == 1) return;//recursion depth
            string word = temp[0];
            temp.insert(temp.begin(), "");
            for (set<string>::iterator j = mapping[word].begin(); j != mapping[word].end(); j++)
            {
                temp[0] = *j;
                getRes(mapping, res, temp, start, length - 1);
            }
        }
    };



  • 相关阅读:
    jQuery库冲突解决办法
    jquery源码 整体架构
    中文版Chrome浏览器不支持12px以下字体的解决方案
    html5 localStorage
    Git创建分支/GIT提交分支
    Git直接拉取远程分支
    vscode关闭后未打开上次界面的解决办法
    MAC升级nodejs和npm到最新版
    hadoop hue切换中文版
    Hdfs dfs命令使用
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6958602.html
Copyright © 2011-2022 走看看