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 ...
随机推荐
- jquery源码学习--iceDog
/** * @FileName : iceDog * @Author : PheonixHkbxoic * @Mail : hkbxoic@gmail.com * @DateTime : 2016-0 ...
- 各种数据处理方案(SQL,NoSQL,其他)的应用场景
综合stackoverflow和linkin上的相关讨论,还有我个人的工作经验: Redis应用场景(大部分场景下memcache可以用Redis代替,所以不单独讨论) 线上业务,读写的高性能要求 ...
- 阿里云利用web直传文件到oss服务器
http://files.cnblogs.com/files/adtuu/oss-h5-upload-js-direct.tar.gz
- js一些实用例子
1.获取焦点选中文本内容 $("#id").focus(function(){ this.select(); }); 2.表单提交方式 A.自动提交 setTimeout(func ...
- redis的数据类型
redis有string,hash,list,sets.zsets几种数据类型 1.string数据类型 可包含任何数据,是二进制安全的,比如图片或者序列化的对象set key valueset na ...
- 成为JAVA GC专家系列
http://www.360doc.com/content/13/0305/10/15643_269387617.shtmlhttp://www.360doc.com/content/13/0305/ ...
- hihoCoder-1000-A+B
题目描述:传统的A+B题 使用语言:C 代码: #include <stdio.h> int main(void){ int a,b; while((scanf("%d %d&q ...
- golang反射初试
golang反射来自Go AST(Abstract Syntax Tree). reflect操作更多像traverse AST. t := reflect.TypeOf(obj) 使用TypeOf( ...
- 解决Autofac MVC 自动注入在 Areas拆分到不同dll下的注入失败问题
由于项目业务复杂,创建了多个Areas 并把他们放在了不同的项目中,项目使用AutoFac做的IOC 配置代码为 public class MvcApplication : System.Web.Ht ...
- 数组和字典 swift
var array = ["A","B"] var array2:[String] = ["A","B"] var ar ...