【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 ...
随机推荐
- Could not delete folder on Win7
I had the same problem on Win 7 and this worked for me in command prompt. Solution 1: RD/S pathname ...
- iOS 拷贝、剪切和粘贴理论基础(转)
简介 在iPhone OS 3.0之后,用户可以在一个应用程序上拷贝文本.图像.或其它数据,然后粘贴到当前或其它应用程序的不同位置上.比如,您可以从某个电子邮件中拷贝一个地址,然后粘贴到Contact ...
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...
- GISer 应届生找工作历程(完结)
有半个月没更博客了,前几天在学校准备这保研,因为没有时间复习高数自然就没有过. 也没啥的,我本来就不打算复习,就是看看运气咋样(哈哈).在昨天开始跑的招聘会,一天下来不同地方跑了三家挺累的,记录 ...
- Java多线程(四) 线程池
一个优秀的软件不会随意的创建.销毁线程,因为创建和销毁线程需要耗费大量的CPU时间以及需要和内存做出大量的交互.因此JDK5提出了使用线程池,让程序员把更多的精力放在业务逻辑上面,弱化对线程的开闭管理 ...
- Angularjs在线编辑器
1.TextAngular: https://github.com/fraywing/textAngular textAngular是一个强大的Text-Editor/Wysiwyg 编辑器,用于An ...
- struts2使用struts2-bootstrap-plugin插件
1.下载插件 http://code.google.com/p/struts2-bootstrap/ 2.添加maven依赖 <dependency> <groupId>com ...
- 导出excel表格
一. 1.获取数据源2.DataTable dt = st.Tables[0]; HttpResponse resp; // HTTP响应信息 resp = Page.Response; resp.C ...
- php计算时间差的方法
一个简单的例子:计算借书的天数,根据每天的日期进行计算. (1) 有数据库的情况 MSSQL可以使用触发器!用专门计算日期差的函数datediff()便可. MYSQL那就用两个日期字 ...
- HTTP的长短连接、长短轮询的区别(转载)
引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此最近经常会看一些源码.在研究一个项目的时候,源码里面用到了HTTP的长轮询.由于之前没太接触过,因此LZ便趁着这个机会,好好了解了一下HT ...