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 ...
随机推荐
- python中split与join
1.split个人最喜欢的就是它能使输入的一连串数字变为list. str=raw_input("some ") str2=str.split(" ") str ...
- 10.python中的序列
本来说完字符串.数字.布尔值之后,应该要继续讲元祖.列表之类的.但是元祖和列表都属于序列,所以有必要先讲讲python的序列是什么. 首先,序列是是Python中最基本的数据结构.序列中的每个元素都分 ...
- C#判断ip地址是否ping的通
Ping pingSender = new Ping(); PingReply reply = pingSender.Send("127.0.0.1",120);//第一个参数为i ...
- webpack 学习笔记 02 快速入门
webpack 的目标 将依赖项分块,按需加载. 减少web app的初始加载时间. 使每一个静态集合都能够作为组件使用. 有能力集成第三方库,作为组件使用. 高度可配置化. 适用于大型项目. INS ...
- Linux环境下的编译,链接与库的使用
参考博客: http://www.cnblogs.com/qytan36/archive/2010/05/25/1743955.html http://m.blog.csdn.net/article/ ...
- android 连续点击退出程序
package com.test.twiceexit; import java.util.Timer; import android.app.Activity;import android.os.Bu ...
- hdu 2275 Kiki & Little Kiki 1
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2275 题意:n个操作 Push 入容器 Pop弹出一个 满足<=该数的最大的数(若没有输出No ...
- HTTP网页错误代码大全带解释
HTTP网页错误代码大全带解释 HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁 ...
- 三星N8000/N8010通用刷机教程
前面已经讲到过如何给三星n8000/n8010 Galaxy Note 10.1获取ROOT权限了.接下来就顺便告诉大家怎么给三星n8000/n8010刷机吧.其实给三星n8000/n8010刷机过程 ...
- TableViewCell Swipe to Delete and More Button(like mail app in iOS7 or later)
在iOS7系统的Mail App中TableViewCell的一个功能让我们做TableView的比较羡慕,就是滑动cell,右边出现了两个按钮,如下: 网上在github上有很多大牛用custom ...