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);
        }
    }
};
时间: 2024-10-12 19:53:28

Word Ladder II [leetcode]的相关文章

Word Ladder II leetcode java

题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: start =

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[python leetcode] Word Ladder II (very hard)[非常难,放弃]

Word Ladder II 描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: • Only one letter can be changed at a time • Each intermediate word must exist in the dictionary For examp

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example,

Word Break II leetcode java

题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit

【leetcode】126. Word Ladder II

题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从startword开始到wordlist每一个word的最小转换次数,这一点非常重要,可以过滤很多无效的运算. 代码如下: class Solution(object): def getLadderList(self, w,d): l = [] r = [] for i in xrange(26): l.a