1. 程式人生 > >Leetcode 127. 單詞接龍 bfs

Leetcode 127. 單詞接龍 bfs

給定兩個單詞(beginWord endWord)和一個字典,找到從 beginWord 到 endWord 的最短轉換序列的長度。轉換需遵循如下規則:

  1. 每次轉換隻能改變一個字母。
  2. 轉換過程中的中間單詞必須是字典中的單詞。

說明:

  • 如果不存在這樣的轉換序列,返回 0。
  • 所有單詞具有相同的長度。
  • 所有單詞只由小寫字母組成。
  • 字典中不存在重複的單詞。
  • 你可以假設 beginWordendWord 是非空的,且二者不相同。

示例 1:

輸入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

輸出: 
5 解釋: 一個最短轉換序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的長度 5。

示例 2:

輸入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

輸出: 0

解釋: endWord "cog" 不在字典中,所以無法進行轉換。

 想了個最簡單的方法,想著可能超時,結果沒超時。。。但是複雜度很高。。。

原因出現在字串查詢是否入隊上和vector選種元素的刪除上。

附上第一次寫的程式碼:
C++:
 

class Solution {
public:
    struct node
    {
        int num;
        string s;
    };
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
         queue<node>q;
         int Size=wordList.size();
         int vis[Size+1];
         memset (vis,0,sizeof(vis));
         node now,next;
         now.num=1; now.s=beginWord;
         q.push(now);
         while (!q.empty())
         {
             now=q.front();
             q.pop();
             if(now.s==endWord)
                 return now.num;
             for (int i=0;i<Size;i++)
                 if(Isone(vis,now.s,wordList[i],i))
                 {
                     next.num=now.num+1;
                     next.s=wordList[i];
                     vis[i]=1;
                     q.push(next);
                 }
         }
        return 0;
    }
    bool Isone (int *vis,string a,string b,int i)
    {
        if(vis[i]==1)
            return false;
        int len=a.size(),diff=0;
        for (int i=0;i<len;i++)
        {
            if(a[i]!=b[i])
                diff++;
            if(diff==2)
                return false;
        }
        if(diff==1)
           return true;
        else 
           return false;
    }
    
};

看了別人的程式碼, 恍然大悟。。 。

然後模仿大佬的程式碼寫了一個, 發現果然快了好多。 。

程式碼如下:

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        queue<string>q;
        map<string,int>m1; //儲存vector中的字串便於查詢是否存在
        map<string,int>re; //儲存結果
        int Size=wordList.size();
        for (int i=0;i<Size;i++)
            m1[wordList[i]]=1;
        re[beginWord]=1;
        q.push(beginWord);
        while ((!q.empty())&&m1.size())
        {
            string now=q.front();
            q.pop();
            int num=re[now];
            int llen=now.size();
            for (int i=0;i<llen;i++)
            {
                string temp=now;
                for (char c='a';c<='z';c++)
                {
                    if(temp[i]==c)
                        continue;
                    else 
                        temp[i]=c;
                    if(m1.find(temp)!=m1.end())
                    {
                      if(temp==endWord)
                        return num+1;
                      q.push(temp);
                      re[temp]=num+1;
                      m1.erase(temp);
                    }
                }
            }
        }
        return 0;
    }
};