【leetcode】Word Ladder (hard) ★
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
思路:
找两点之间的最小距离,感觉只能用图,构建图的邻接矩阵,然后folyd 果断的超时了.....
int ladderLength1(string start, string end, unordered_set<string> &dict) {
if(start == end) return ;
int vexnum = dict.size() + ;
if(dict.find(start) != dict.end()) vexnum--;
if(dict.find(end) != dict.end()) vexnum--;
vector<string> s(vexnum); //记录graph中每行代表的单词
vector<vector<int>> graph(vexnum, vector<int>(vexnum, vexnum + )); //邻接矩阵
s[] = start;
s.back() = end;
int i = ;
unordered_set<string>::iterator it;
for(it = dict.begin(); it != dict.end(); it++)
{
if(*it != start && *it != end)
s[i++] = (*it);
} //记录有哪些单词可以通过一次变化相互转换
for(i = ; i < s.size(); i++)
{
string temp = s[i];
for(int j = ; j < start.size(); j++)
{
for(char c = 'a'; c <= 'z'; c++)
{
temp[j] = c;
if(dict.find(temp) != dict.end())
{
int edge = find(s.begin(), s.end(), temp) - s.begin();
if(edge == i)
{
graph[i][edge] = ;
graph[edge][i] = ;
}
else
{
graph[i][edge] = ;
graph[edge][i] = ;
}
}
}
}
} for(int k = ; k < graph.size(); k++)
{
for(int i = ; i < graph.size(); i++)
{
for(int j = ; j < graph.size(); j++)
{
if(graph[i][j] > graph[i][k] + graph[k][j])
{
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
} return (graph[][vexnum - ] == vexnum + ) ? : graph[][vexnum - ] + ;
}
来看大神的BFS算法,用dis记录每个点到start的距离。队列里开始只有start, 然后每次遇到新转化成的单词就进队列,更新距离。判断能否转化时用字符长度和26个字母,而不是对字典遍历,因为字典中单词的数量可能远大于26个。
int ladderLength(string start, string end, unordered_set<string> &dict)
{
unordered_map<string, int> dis;
queue<string> q;
dis[start] = ;
q.push(start);
while(!q.empty())
{
string word = q.front(); q.pop();
for(int i = ; i < start.size(); i++)
{
string temp = word;
for(char c = 'a'; c <= 'z'; c++)
{
temp[i] = c;
if(dict.count(temp) > && dis.count(temp) == )
{
dis[temp] = dis[word] + ;
q.push(temp);
}
}
}
}
if(dis.count(end) == ) return ;
return dis[end];
}
【leetcode】Word Ladder (hard) ★的更多相关文章
- 【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 ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【leetcode】Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
随机推荐
- asp.net 文件操作小例子(创建文件夹,读,写,删)
静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作 一.创建文件夹 using System.IO; string name = "aa"; strin ...
- jqGrid API 相关
取消所有选中的行: $("jqgridtableid").trigger("reloadGrid"): 设定选中行,可设定多行选中: $("jqgri ...
- 《HTML5网页开发实例详解》连载(四)HTML5中的FileSystem接口
HTML 5除了提供用于获取文件信息的File对象外,还添加了FileSystem相关的应用接口.FileSystem对于不同的处理功能做了细致的分类,如用于文件读取和处理的FileReader和Fi ...
- poj 3614 Sunscreen
...
- iOS 非ARC基本内存管理系列 5-autorelease方法使用总结
autorelase:可以将对象交给自动释放池中,释放池销毁的时候对里面的对象做一次release操作代码如下 @autoreleasepool { Person *person = [[[Perso ...
- 关于C#正则表达式MatchCollection类的总结,正则表达式的应用
认识MatchCollection 类 表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合. 命名空间: System.Text.RegularExpressions 属性:C ...
- DTcms手机版使用余额支付 提示信息跳转到PC版的错误。以及提交订单不打开新页面
手机版使用余额支付 提示信息跳转到PC版的错误 引起错误的原因是中间需要提交到DTcms.Web\api\payment\balance\index.aspx去处理 导致BasePage.cs中的li ...
- 【转】Qt使用自带的windeployqt 生成exe来发布软件
集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application 和 Qt Quick Application.下面分别介绍这两类exe ...
- hadoop 文件 复制 移动 FileUtil.copy
FileSystem fs = FileSystem.get(conf); Path pattern = new Path(resultViewPath + "phone*"); ...
- js鼠标及对象坐标控制属性详细解析
对js鼠标及对象坐标控制属性进行了详细的分析介绍. offsetTop获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算顶端位置. offsetLeft获取对象相对于版面或由 ...