码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 126. 单词接龙 II

时间:2018-08-09 12:16:07      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:oid   amp   back   cond   pat   pair   code   it!   map   

class Solution {
public:    
    void getShortestPaths(string cur,
                      string ed,
                      map<string, vector<string>> &nexts,
                      map<string, int> &distances,
                      vector<string> &solution,
                      vector<vector<string>> &res)
    {
            solution.push_back(cur);
            if (cur == ed)
            {
                res.push_back(solution);
            }
            else
            {
                auto vec_it = nexts.find(cur)->second;
                for(auto it=vec_it.begin(); it!= vec_it.end(); ++it)
                {
                    if(distances.find(*it)->second == distances.find(cur)->second + 1)
                        getShortestPaths(*it, ed, nexts, distances, solution, res);
                }
            }
            solution.pop_back();
    }


    map<string, int> getDistances(string beg, map<string, vector<string>> &nexts)
    {        
        map<string, int> distance;
        set<string> aux;
        queue<string> que;
        que.push(beg);
        distance.insert(make_pair(beg, 0));
        aux.insert(beg);
        while(!que.empty())
        {
            string tmp = que.front();
            que.pop();
            auto vec_it = nexts.find(tmp)->second;
            for(auto it = vec_it.begin(); it!= vec_it.end(); ++it )
            {              
                if(aux.find(*it) == aux.end())
                {
                    distance.insert(make_pair(*it,  distance.find(tmp)->second+1 ));
                    que.push(*it);
                    aux.insert(*it);
                }
            }
        }        
        return distance;
    }

    vector<string> getNext(string word, const set<string> &dict)
    {
        vector<string> res;
        for(int i=0; i<word.size(); ++i)
        {
            for(char cur = a; cur <= z; ++cur)
            {
                if(word[i] != cur)
                {
                    string tmp = word;
                    tmp[i] = cur;
                    if(dict.find(tmp) != dict.end())
                    {
                        res.push_back(tmp);
                    }
                }
            }
        }
        return res;
    }

    map<string, vector<string>> getNexts(const vector<string> &words)
    {
        set<string> dict(words.begin(), words.end());
        map<string, vector<string>> nexts;
        for(int i=0; i<words.size(); ++i)
        {
            nexts.insert(make_pair(words[i], getNext(words[i], dict)));
        }
        return nexts;
    }

    
    vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        wordList.push_back(beginWord);
        auto nexts = getNexts(wordList);
        auto distances = getDistances(beginWord, nexts);
        vector<string> path;
        vector<vector<string>> res;
        getShortestPaths(beginWord, endWord, nexts, distances, path, res);
        return res;
    }

};

 

Leetcode 126. 单词接龙 II

标签:oid   amp   back   cond   pat   pair   code   it!   map   

原文地址:https://www.cnblogs.com/randyniu/p/9447782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!