【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 ...
随机推荐
- 浏览器userAgent大全
iPhone ●iOSMozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Versi ...
- 在WCF中使用Flag Enumerations
请看MSDN示例: [DataContract][Flags] public enum CarFeatures { None = 0, [EnumMember] AirCo ...
- int/double/string使用
在计算机中存储数据和儿童在抽屉中存放物品很类似. 例如: 要在计算机中存一个数字50,需要两句话. int a; //将要放的物品告诉家长 a=50; //将物品放到某个抽屉中 计算机存储变量的过 ...
- System.Windows.Forms.Timer
一.主要属性.方法和事件 Windows 窗体 Timer 是定期引发事件的组件.该组件是为 Windows 窗体环境设计的. 时间间隔的长度由 Interval 属性定义,其值以毫秒为单位.若启用了 ...
- SQL server 常见用法记录
-- ============================================= -- Author: tanghong -- Create da ...
- Unity基本操作一
1,使对象进入摄像机镜头内align with view 2,太阳光 创建点point light,调节Intensity改变光照强度,上面的Range改变光照范围. 3,Animation选中Pla ...
- VS2010
1,vc++目录——>包含目录: Visual Studio will search for the include files referred to in your source code ...
- From MSI to WiX, Part 4 - Features and Components by Alex Shevchuk
Following content is directly reprinted from : http://blogs.technet.com/b/alexshev/archive/2008/08/2 ...
- hash桶
#include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...
- android SDK Manager更新不了,出现错误提示:"Failed to fetch URL..."!
可以用以下办法解决: 使用SDK Manager更新时出现问题 Failed to fetch URL https://dl-ssl.google.com/android/repository/rep ...