leetcode 137单词接龙

直接层序遍历,结果有部分测试样例超时;
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
//利用二叉树的层序遍历;
if(beginWord.size()!=endWord.size()) return ;
unordered_map<string,int> h;
for(int i=;i<wordList.size();i++){
h[wordList[i]]=;
}
if(h[endWord]==) return ;
int level=;
queue<string> q;
q.push(beginWord);
while(!q.empty()){
level++;
int qlen=q.size();
while(qlen--){
string front=q.front();
//cout<<front<<",";
q.pop();
for(int i=;i<wordList.size();i++){
if(h[wordList[i]]==) continue;
if(dis(front,wordList[i])==){
if(wordList[i]==endWord) return level;
q.push(wordList[i]);
h[wordList[i]]=;
}
}
}
//cout<<endl;
}
return ;
}
int dis(string w1,string w2){
if(w1.size()!=w2.size()) return ;
int res=;
for(int i=;i<w1.size();i++){
res+=(w1[i]-w2[i]==)?:;
if(res>) return res;
}
return res;
}
};
究其原因,是因为距离计算每次都要调用函数过于复杂,由于两两单词间计算距离,并且计算距离时又需要对每个字母进行遍历,因此timeO(n^2*m)
改变距离的计算,对其做预处理,列出每个单词的状态,比如hog 可列为 *og,h*g,ho*;通过临接表来表示,即一个键值(key)为状态,值(value)为hog,即unordered_map<string,set<string>> m(单词状态,单词列表) 对n个词,每个词m个状态进行检索, time O(mn)级别,
C++代码如下:
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
//处理边界情况;
if(beginWord.size()!=endWord.size() || wordList.size()== || wordList[].size()==) return ;
int lr=wordList.size(),lc=wordList[].size();
//初始化哈希表h,记录访问情况;
unordered_map<string,int> h;
for(int i=;i<lr;i++){
h[wordList[i]]=;
}
//预处理:初始化状态表m(状态,单词列表)
unordered_map<string,set<string> > m;
for(int i=;i<lr;i++){
for(int j=;j<lc;j++){
string tmp=wordList[i];
tmp[j]='*';
m[tmp].insert(wordList[i]);
}
}
//BFS搜寻最短路径
int level=;
queue<string> q;
q.push(beginWord);
while(!q.empty()){
level++;
int qsize=q.size();
while(qsize--){
string front=q.front();
q.pop();
for(int i=;i<lc;i++){
string state=front;
state[i]='*';
for(string child: m[state]){
if(h[child]==) continue;
//cout<<child<<",";
if(child==endWord) return level;
h[child]=;
q.push(child);
}
}
}
//cout<<endl;
}
return ;
}
};
leetcode 137单词接龙的更多相关文章
- Leetcode 126.单词接龙II
单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...
- Java实现 LeetCode 127 单词接龙
127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...
- Java实现 LeetCode 126 单词接龙 II
126. 单词接龙 II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: ...
- leetcode 127 单词接龙
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...
- [LeetCode] 126. 单词接龙 II
题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...
- Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)
Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- NOIP2000单词接龙[DFS]
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
- Noip2000 T3 单词接龙
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
随机推荐
- kafka核心原理总结
新霸哥发现在新的技术发展时代,消息中间件也越来越受重视,很多的企业在招聘的过程中着重强调能够熟练使用消息中间件,所有做为一个软件开发爱好者,新霸哥在此提醒广大的软件开发朋友有时间多学习. 消息中间件利 ...
- poj 1953 World Cup Noise (dp)
World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16774 Accepted: 8243 ...
- Linux系统组成和获取命令帮助1
在GNU上边发布的都是源码,不可以直接拿来使用 源代码都是文本格式的,需要找个编译器编译成不同机器上使用的二进制,这样机器才可以运行的起来 英特儿的CPU有着x86,x64架构之分,x64又叫amd6 ...
- html 中 图片和文字一行 垂直居中对齐
效果: 代码:<div><img src='img/point_icon.png' width='35px' height='35px' style='float: lef ...
- Gym - 101915D Largest Group 最大团
给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bit ...
- vue admin element
vue.package.js 修改 publicPath: './',
- vue启动问题(You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.)
解决vue启动出现: 在build/webpack.base.conf.js文件中,把...(config.dev.useEslint ? [createLintingRule()] : [])注释或 ...
- Here is a test page for my new blog in cnblogs
Tell me if I can use Fomula like LaTeX $$\sum\limits_{i = 1}^{n}a_i$$
- head first 设计模式笔记5-单例模式
目录: 1.单例模式(Singleton Pattern) 2.概念 3.饿汉式:不是延迟加载,加载类的时候直接初始化 4.懒汉式:延迟加载,首次需要使用的时候在实例化,需要考虑线程安全 5.静态内部 ...
- 【LOJ2604】「NOIP2012」开车旅行
[题目链接] [点击打开链接] [题目大意] 从西到东的坐标轴\([1,n]\)上有\(n\)个海拔互不相同的城市,每两个城市之间的距离定义为\(dis(i,j)=|h_i-h_j|\) 小\(A\) ...