LeetCode127. 单词接龙

力扣

 

解题思路: 像这种BFS类型的题都有类似的模板

1.  通过 BFS, 首先用 beginWord 带出转换一个字母之后所有可能的结果
2. 每一步都要把队列中上一步添加的所有单词转换一遍,最短的转换肯定在这些单词当中, 所有这些词的转换只能算一次转 换,因为都是上一步转换出来的,这里对于每个单词的每个位置都可以用26 个字母进行转换,所以一个单词一次转换的可能 有:单词的长度 * 26
3.  把转换成功的新词入队,进行下一步的转换
4.  最后整个转换的长度就和 BFS 执行的次数相同

优化:
①  使用unordered_set<string>  存储 字典中的单词,查找效率高
     使用unordered_set<string>  存储 已经查找过的单词,防止死循环。

②    如果使用两个同时进行的广搜可以有效地减少搜索空间。一边从 beginWord 开始,另一边从 endWord 开始。我们每次从两边各扩展一层节点,当发现某一时刻两边都访问过同一顶点时就停止搜索

更好的理解 ->  B站 : 花花酱

1.单向BFS

class Solution 
{
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) 
    {
       //hash表的查询效率最高 
       unordered_set<string> wordDict(wordList.begin(), wordList.end()); 
       
       unordered_set<string> visited; //标记单词是否已经访问过,访问过的不再访问 
       visited.insert(beginWord); 
       
       queue<string> q; //初始化队列 
       q.push(beginWord); 

       int res = 1; 
       while (!q.empty())
       { 
           int nextSize = q.size(); 
           //每一步都要把队列中上一步添加的所有单词转换一遍 
           //最短的转换肯定在这些单词当中, 所有这些词的转换只能算一次转换

         //因为都是上一步转换出来的 
         while (nextSize--)
         { 
             string curWord = q.front(); 
             q.pop(); 

             if(curWord == endWord)
             return res;
             
             for (int i = 0; i < curWord.size(); i++)//尝试转换当前单词的每一个位置 
             { 
                 string newWord = curWord; 
                 
                 for (char ch = 'a'; ch <= 'z'; ch++)//每一个位置用26个字母分别替换 
                 {
                      newWord[i] = ch; 
                     
                      //判断新的单词是否在词典中,且没有搜索过
                      if (wordDict.find(newWord) != wordDict.end()
                      && visited.find(newWord) == visited.end()) 
                      {
                          visited.insert(newWord); //标记已使用
                          q.push(newWord); 
                      }
                      
                 } 
                 
             }

          }
          ++res; //转换成功  
        }

        //转换不成功,返回0 
        return 0;
    }
};

2.双向BFS

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) 
    {
      //双向广度优先搜索
      unordered_set<string> dict(wordList.begin() , wordList.end());

      if(dict.find(endWord) == dict.end())
      return 0;
      
      // 分别用左边和右边扩散的哈希表代替单向 BFS 里的队列,它们在双向 BFS 的过程中交替使用
      unordered_set<string> q1{beginWord}; //从前向后搜索
      unordered_set<string> q2{endWord}; //从后向前搜索
      int len = beginWord.size();

      //执行双向 BFS,左右交替扩散的步数之和为所求
     int ans = 0;
      while(!q1.empty() && !q2.empty())
      {
          ++ans;

         // 优先选择小的哈希表进行扩散,考虑到的情况更少
          if(q1.size() > q1.size())
          std::swap(q1,q2);

          unordered_set<string> q;//遍历原来哈希表中的单词 ,产生的新的序列保存在这里
         
         //这里遍历每一个节点的单词,然后修改其中一个字符,让他成为一个新的单词,
        //并查看这个新的单词在字典中是否存在,如果存在并且没有被访问过,就加入到队列中
         for(string str : q1)
         {
             for(int i = 0 ; i < len ;++i)
             {
                 char ch = str[i];
                 for(char ch = 'a' ; ch <= 'z' ;++ch)
                 {
                        str[i] = ch;
                        if (q2.find(str) != q2.end()) return ans + 1;
                        if (dict.find(str) == dict.end()) continue;

                        dict.erase(str);
                        q.insert(str);
                 }
                 str[i] = ch;
             }
         }

         std::swap(q , q1);//将新的序列和老的序列交换

      }

      return  0 ;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值