直接层序遍历,结果有部分测试样例超时;

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单词接龙的更多相关文章

  1. Leetcode 126.单词接龙II

    单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...

  2. Java实现 LeetCode 127 单词接龙

    127. 单词接龙 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字 ...

  3. Java实现 LeetCode 126 单词接龙 II

    126. 单词接龙 II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: ...

  4. leetcode 127 单词接龙

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...

  5. [LeetCode] 126. 单词接龙 II

    题目链接 : https://leetcode-cn.com/problems/word-ladder-ii/ 题目描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordL ...

  6. Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)

    Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  7. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  8. NOIP2000单词接龙[DFS]

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  9. Noip2000 T3 单词接龙

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

随机推荐

  1. Inception网络模型

    最近在研究inception模型,将v1到v4版本的论文都研读了一下,这里做一下总结. 这里推荐一下这个GitHub,博主将常见的论文都做了翻译,大家可以参考中文来加深理解. 1.Inception ...

  2. pyquery:轻松、灵活的处理html

    介绍 pyquery是一个专门用来解析html的库,从名字很容易想到jQuery,没错,这完全是仿照jQuery的语法实现的.如果用过jQuery,那么pyquery也很容易上手 初始化html py ...

  3. 网络初级篇之OSPF(一)原理

    一.OSPF是什么 Open Shortest Path First, 开放最短路径优先协议,是一种开源的使用最短路径优先(SPF)算法的内部网关协议(IGP).常用于路由器的动态选路. 二.OSPF ...

  4. ISO/IEC 15444-12 MP4 封装格式标准摘录 2

    目录 Track Media Structure Media Box Media Header Box Handler Reference Box Media Information Box Medi ...

  5. 004-linux下配置rsyslog日志收集服务器案例 rsyslog+loganalyzer日志服务器,无法添加报表模板解决

    centos6系统 client1:192.168.1.33 centos7系统 client2:192.168.1.44 centos7系统 master:192.168.1.55 配置服务端mas ...

  6. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 百度网盘,FTP上传异常、上传失败的解决办法

    若你的宽带上传上限速度为50KB,那么将百度网盘或FTP的上行速度调为50KB以下即可,就不会出现网络异常的情况了.

  8. 【HEOI2015】小Z的房间

    题意 https://www.luogu.org/problemnew/show/P4111 题解 前置知识:矩阵树定理 不要问证明,我不会,用就完事了(反正一般也不会用到) 因为矩阵树定理就是求一张 ...

  9. Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers

    D. Arpa and a list of numbers time limit per test   2 seconds memory limit per test     256 megabyte ...

  10. 清北学堂提高组突破营考试T1

    题目如下: (想要作弊的后几届神仙们我劝你们还是别黈了,这个题如果你们不会只能证明你们上错班了). 好,题目看完了,发现是一道大模拟(%你)题,于是我们按照题目说的做: #include<ios ...