Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWord 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.
class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
if(beginWord.size() != endWord.size()) return ; unordered_set<string> visited;
int level = ;
bool found = ;
queue<string> current,next; current.push(beginWord);
while(!current.empty() && !found){
level++;
while(!current.empty() && !found){
string str(current.front());
current.pop();
for(size_t i=;i<str.size();i++){
string new_word(str);
for (char c = 'a';c <= 'z';c++){
if (c == new_word[i]) continue; swap(new_word[i],c);
if(new_word == endWord){
found = ;
break;
} if(wordList.count(new_word) && !visited.count(new_word)){
visited.insert(new_word);
next.push(new_word);
}
swap(new_word[i],c);
}
}
}
swap(current,next);
}
if(found) return level+;
else return ; }
};
Word Ladder的更多相关文章
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- php include
get_include_path 获取当前 include_path 配置选项的值,在当前代码目录未找到include文件时,则到include_path去include. set_include_ ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- Chrome 开发者工具有了设备模拟器
今天从哥们那里学到了一个小技巧,使用chrome自带的多设备模拟器来调试页面在不同设备下的显示效果. 特地上网查了一下,记录一下. 如果想要在 Chrome 上测试网站在不同设备,不同分辨率的显示情况 ...
- 原子操作 Interlocked系列函数
上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...
- Xrun 将 app 转化为 IPA
xcodebuild命令行打包,在使用xcodebuild编译后发现有些东西有些临时性质的东西,依然存在,搜索了一些资料,找到有clean的命令:在之前打包都是生成app文件,将app打包成ipa文件 ...
- dom添加事件
1.语法:document.getElementById('btn').addEventListener 2.可以添加多个EventListener,且不会覆盖 3.移除EventListener, ...
- 《day16_多线程细节_Eclipse使用》
多线程的一些细节: 1,面试题:sleep方法和wait方法异同点是什么? 相同点:可以让线程处于冻结状态. 不同点: 1, sleep必须指定时间. wait可以指定时间,也可以不指定时间. 2, ...
- Linux下tmpfs与ramfs的区别
ramfs是Linux下一种基于RAM做存储的文件系统.在使用过程中你就可以把ramfs理解为在普通的HDD上建立了一个文件系统,而现在HDD被替换成了RAM,因为是RAM做存储所以会有很高的存储 ...
- <em>标签
<em> 标签告诉浏览器把其中的文本表示为强调的内容.对于所有浏览器来说,这意味着要把这段文字用斜体来显示.
- hdu 2086
PS:推算...数组如果开得不够大也会超时... 代码: #include "stdio.h" double cal(int t,double a[]); int main(){ ...