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 ...
随机推荐
- springmvc学习笔记---idea创建springmvc项目
前言: 真的是很久没搞java的web服务开发了, 最近一次搞还是读研的时候, 想来感慨万千. 英雄没落, Eclipse的盟主地位隐隐然有被IntelliJ IDEA超越的趋势. Spring从2. ...
- java的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- Codeforces Round #280 (Div. 2) A B C 暴力 水 贪心
A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Fragment中调用Activity的UI
1:Fragment内定义一个接口,或外部定义一个接口, 2:Fragment定义一个接口对象的属性 3:Activity实现这个接口 4:Fragment的onAttach方法中强转Activity ...
- Web端文件打包.zip下载
使用ant.jar包的API进行文件夹打包.直接上代码: String zipfilename = "test.zip"; File zipfile = new File(zipf ...
- JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能
一.分页工具类 package blank.util;import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; ...
- python设置字体颜色
在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信 ...
- AWR分析。(shared_pool,sga_size大小设置)
Execute to Parse 指标反映了执行解析比 其公式为 1-(parse/execute) , 目标为100% 及接近于只 执行而不解析 在oracle中解析往往是执行的先提工作,但是通过游 ...
- android读取远程图片案例
关键代码:Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);imageview.setImageBitmap(bitm ...
- EntityValidationErrors
关于如何查看 EntityValidationErrors 详细信息的解决方法 我们在 EF 的编程中,有时候会遇到这样一个错误: 但是,按照他的提示 "See 'EntityValidat ...