126单词接龙2

相较于单词接龙1,此题主要难点在于记录路径。
BFS适合找最短的深度,但是无法记录路径。

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> wordSet(wordList.begin(),wordList.end());
        if(wordSet.find(endWord)==wordSet.end()) return 0;
        unordered_map<string,int> visitMap;
        queue<string> que;
        que.push(beginWord);
        visitMap.insert(pair<string,int>(beginWord,1));
        while(!que.empty()){
            string word=que.front();
            que.pop();
            int path=visitMap[word];
            for(int i=0;i<word.size();i++){
                string newWord=word;
                for(int j=0;j<26;j++){
                    newWord[i]=j+'a';
                    if(newWord==endWord) return path+1;
                    if(wordSet.find(newWord)!=wordSet.end()&&visitMap.find(newWord)==visitMap.end()){
                        visitMap.insert(pair<string,int>(newWord,path+1));
                        que.push(newWord);
                    }
                }
            }
        }
        return 0;
    }
};

鉴于此采用dfs+bfs的思路

class Solution{
    vector<vector<string>> res;
    unordered_map<string,vector<string>> neighbor;
public:
    vector<vector<string>> findLadders(string beginWord,string endWord,vector<string>& wordList){

       unordered_set<string> wordSet(wordList.begin(),wordList.end());
       if(wordSet.find(endWord)==wordSet.end()) return res;

        unordered_map<string,int> depth;
        depth[beginWord]=1;
        queue<string> que;
        que.push(beginWord);
        while(!que.empty()){
            string word=que.front();
            que.pop();
            for(int i=0;i<word.size();i++){
                string temp=word;
                for(int j=0;j<26;j++){
                    temp[i]=j+'a';
                    if(wordSet.count(temp)){
                        if(depth.count(temp)==0){
                            que.push(temp);
                            depth[temp]=depth[word]+1;
                            neighbor[temp].push_back(word);
                        }else if(depth[temp]==depth[word]+1){
                            neighbor[temp].push_back(word);
                        }
                    }
                }
            }
        }
        vector<string>path;
        dfs(beginWord,endWord,path);
        return res;
    }
    void dfs(string& beginWord,string& curr,vector<string> path){
        if(curr==beginWord){
            path.push_back(curr);
            reverse(path.begin(),path.end());
            res.push_back(path);
        }
        path.push_back(curr);
        for(auto word:neighbor[curr]) {
            dfs(beginWord,word,path);
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值