Leetcode#127 Word Ladder
BFS
Word Ladder II的简化版(参见这篇文章)
由于只需要计算步数,所以简单许多。
代码:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if (start == end)
return ;
unordered_set<string> old;
queue<string> layer;
int len = ;
layer.push(start);
while (!layer.empty()) {
queue<string> nextLayer;
while (!layer.empty()) {
string str = layer.front();
layer.pop();
if (str == end)
return len;
for (int i = ; i < str.length(); i++) {
for (char j = 'a'; j <= 'z'; j++) {
string next = str;
next[i] = j;
if (old.find(next) == old.end() && dict.find(next) != dict.end()) {
old.insert(next);
nextLayer.push(next);
}
}
}
}
len++;
layer = nextLayer;
}
return ;
}
Leetcode#127 Word Ladder的更多相关文章
- [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、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 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 (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- 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单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
随机推荐
- webview 中 svg的坑
在这里不会详细介绍如何绘制svg图片,是讲一个很小的bug,看图 在这张图中,上面带有纹理和弧度的图片,原本是直接切了一张png的图片,但是由于是在app的登录注册的首页,那么这个35k的图片就会非常 ...
- 百度分享如何自定义分享url和内容?
百度分享默认分享的是当前页的url,但也可以在同一个页面中分享多个不同的url,仅需进行如下简单的配置. 默认的代码如下: <div id="bdshare" class=& ...
- windows下 apache 二级域名相关配置
小编今天给大家总结下 windows 下 apache的二级域名的相关配置 利用.htaccess将域名绑定到子目录 下面就利用本地127.0.0.1进行测试 我们这里以 www.jobs.com 为 ...
- (转)Android属性设置android:noHistory="true"
设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...
- Delphi7中编译提示“Unsafe type 'PChar'”的原因及处理办法
delphi7中加入了对.net的支持 在.net中是没有指针的(托管环境中),所以指针都是不安全的,不符合.net规范 所以d7里有警告,可以不管它 DELPHI7已经考虑到了移植到点NET的问题, ...
- javascript中方法调用与方括号[]
看jquery时遇到一行: $(this)["removeClass"]("selected"); 这一行等同于下面的一行: $(this).removeCla ...
- Moses更改权重的命令变化 -d -t -
-l 可以用: weight-l 或者lm (不需要在前面加-) 还是用-weight-overwrite “Distortion0= 0"更保险 reording weight i ...
- Java入门到精通——基础篇之面向对象
一.概述. Java属于面向对象的一种语言,因为Java是面向对象的语言所以这个语言的诞生需要有五个基本特性: 1)万物皆为对象. 2)程序是对象的集合. 3)每个对象都有自己的由其他对象所构成的存储 ...
- 算法系列7《CVN》
计算CVN时使用二个64位的验证密钥,KeyA和KeyB. 1) 计算CVN 的数据源包括: 主账号(PAN).卡失效期和服务代码,从左至右顺序编排. 4123456789012345+8701+11 ...
- hdu 5101 Select
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5101 Select Description One day, Dudu, the most cleve ...