【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 ...
随机推荐
- javascript对象初读
<script type="text/javascript"> function baseClass() { this.showMsg = function() { a ...
- Objective-c中的对象间的消息传递以及消息路由
刚开始使用Objective-C时,总是习惯将对象间发送消息之间称呼为方法调用.心想,这和c#不是一回事吗?不就是调用实例方法吗,还搞个消息发送作甚,最后还不是要转化为方法的调用?通过一段时间的理解学 ...
- iOS开发——app审核指导方针(官网)
iOS 开发后上传到App Store审核的指导方针 ——苹果官网介绍地址 https://developer.apple.com/app-store/review/guidelines/
- Visual Studio的MethMVVM
MethMVVM介绍: Visual Studio Gallery是微软针对VisualStudio扩展提供的一种解决方案,在Visual Studio Gallery你能够找到各种不同主题的解决方案 ...
- Apache 安装与配置
安装Apache 1.下载apache软件http://www.apache.org 2.安装 以管理员身份进行安装 3.设置主机名与域名 4.选择安装类型 5.自定义安装路径 6.开始安装 7.安装 ...
- 使用FTP删不掉文件的解决方法
今天在清理自己的服务器的时候发现曾经上传了一些png文件,中文命名的,权限是718,如果权限为777就可以删掉但是很奇怪的是执行权限修改也不行,改不掉: 最后的解决方法就是使用windows 随便打开 ...
- js 的数据类型转换
一直对js的类型转换一直半解,今天理一下思路,首先说一下几个特殊的数值 null null是特殊的object,故 typeof null 返回object, null派生于undefined ,故 ...
- IE6/IE7中li底部4px的Bug
当li的子元素中有浮动(float)时,IE6/IE7中<li>元素的下面会产生4px空隙的bug. XHTML <ul class="list"> < ...
- Linux 查看某个用户的进程
Linux 查看某个用户的进程 To view only the processes owned by a specific user, use the following command: top ...
- opencv学习笔记(02)——遍历图像(指针法)
#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <ope ...