LeetCode 127. Word Ladder

127. Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the the same.

题目大意:给出beginWord、endWord和一个字典,找到从beginWord到endWord的最短转换序列,转换要求是:
1.每次只能改变一个字母~
2.变换过程中的中间单词必须在字典中出现~(第一个beginWord不需要出现,最后一个endWord需要在字典中出现~)
分析:用广度优先搜索~先将beginWord放入队列中,然后将队列中的每一个单词从头到尾换26个字母一遍~如果换后的单词在字典中能找到~而且没有被访问过~(如果每次都找访问过的就死循环啦,不停的变来变去变同一个咋办~)那就将这个单词放入队列中继续变换~直到有一次发现在字典中找到单词的时候,这个单词恰好是endWord为止~
因为要返回路径长度~所以在队列中放一个string和int组成的pair一对~这样的话用string表示单词,int表示变换到当前单词的路径~比如startWord就是1~之后每次加1~因为题目给的是vector~把他们所有单词先放到dict的set集合中查找单词会方便很多~visit标记当前单词是否被访问过~

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        set<string> dict, visit;
        for(int i = 0; i < wordList.size(); i++)
            dict.insert(wordList[i]);
        queue<pair<string, int>> q;
        q.push(make_pair(beginWord, 1));
        while(!q.empty()) {
            pair<string, int> temp = q.front();
            q.pop();
            string word = temp.first;
            for(int i = 0; i < word.length(); i++) {
                string newword = word;
                for(int j = 0; j < 26; j++) {
                    newword[i] = 'a' + j;
                    if(dict.find(newword) != dict.end() && visit.find(newword) == visit.end()) {
                        if(newword == endWord)
                            return temp.second + 1;
                        visit.insert(newword);
                        q.push(make_pair(newword, temp.second + 1));
                    }
                }
            }
        }
        return 0;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值