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 intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
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.
====================
广度优先搜索bfs,
题目需要注意的地方:
- 如果没有可以转变的单词阶梯,需要返回-1,
- 所有的单词由相同的长度
- 所有的单词都是小写字母
- 转换过程中,一次只能转变一个字母
- 输入有开始单词,有结束单词,还有一个词典集合set,
- 转变过程中的所有 中间单词表示 都是从词典集合set中存在的
- 输出的是 单词阶梯的长度(包括开始单词和结束单词)
经典bfs搜索
- 定义一个unorder_map<string,int> m; 保存的是在转变过程中的 中间单词出现的位置,即"hit" -> "hot" -> "dot" -> "dog" -> "cog",m[hit]=1,m[hot]=2
- 定义一个queue<string> q;保存的是在广度优先搜索bfs的搜索次序,因为bfs一定会用到队列,就像深度优先搜索dfs一定会用到栈一样。这个队列q会把搜索路径中所有经过的 单词中间节点 都过滤一边。
- 因为我们只是求单一的解,找到即结束。所以不用保存搜索路径,不用找到所有的结果。
- 看代码。
int ladderLength(string beginWord, string endWord,
unordered_set<string>& wordList) {
unordered_map<string,int> m;
queue<string> q;
m[beginWord] = ;
q.push(beginWord);
while(!q.empty()){
string word = q.front();
q.pop();
for(int i = ;i<(int)word.size();i++){
string newWord = word;
for(char ch = 'a';ch<='z';++ch){///因为我们每次只能改变一个字母,所以这里是一重循环遍历'a'->'z',看是否能在字典集合中找改变后的 单词值
newWord[i] = ch;///这就是改变一个字母后的单词值
if(newWord==endWord) return m[word]+;///此时已经能够找到一条路径了,直接返回就行了。
if(wordList.find(newWord)!=wordList.end() && ///在字典集合中存在,
m.find(newWord)==m.end()){///改变一个字母后的单词在 哈希表m中不存在,是为了防止出现hit->hit的情形。
//cout<<"q.size()="<<q.size()<<endl;
//cout<<"word->"<<word<<endl;
//cout<<"newWord->"<<newWord<<endl<<endl;
q.push(newWord);///放入q中,为bfs做准备
m[newWord] = m[word]+;///新单词在 单词阶梯中的位置
}
}///for
}///for
}
return ;
}
127 Word Ladder的更多相关文章
- 127. Word Ladder(M)
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- Map-API及详解
常用API 类别 方法 增加 put.putAll 删除 remove.removeAll.clear 查询 get 取值 entrySet.keySet.values 长度 size 判断 cont ...
- 浅谈C中的malloc和free
转自http://bbs.bccn.net/thread-82212-1-1.html非常感谢作者 浅谈C中的malloc和free 在C语言的学习中,对内存管理这部分的知识掌握尤其重要!之前对C中的 ...
- c++有默认参数的函数---4
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 1.默认参数的目的 C++可以给函数定义默认参数值.通常,调用函数时,要为函数的每个参数给定对应的实参. ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- POJ1459 Power Network(网络最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Total S ...
- 神奇的输入 while(cin>>....)如何在遇见换行之后进入下一层循环读入
cin>>m>>n; ;i<=m;i++) { ; char ch=' '; ) //在遇到换行之后进入下一层循环读入. { x++; cin>>c[x]; ...
- ARM的工作模式和寄存器
以前学的时候学的是S3C6410的开发板,它是三星公司推出的基于ARM v6架构(指令集),处理器是ARM11. ARM架构是构建每个ARM处理器的基础. 目前最新的是ARM v8架构:http:// ...
- 论文笔记之:Active Object Localization with Deep Reinforcement Learning
Active Object Localization with Deep Reinforcement Learning ICCV 2015 最近Deep Reinforcement Learning算 ...
- FreeSWITCH在呼叫失败的情况下播放语音提示
看到好多网友问到这个问题.一般我们在打电话时会听到“您拨的电话正在通话中,请稍后再拨....”,或“电话无应答...”之类的提示,我们在 freeswitch 里也可以这样做. 其实很简单,默认的配置 ...
- Linux-NFS原理介绍
NFS(network filesystem)是由sun公司开发的,其作用是在网络当中可以将想要开发的目录共享给别人,这样使得访问者访问nfs服务器上的东西就像访问本地的文件一样,在将文件公开给别人的 ...