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 单词接龙
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...
随机推荐
- Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历
package com.example.demo; public class BTree { public int data; public BTree left; public BTree rigt ...
- 5.flask与数据库
1.安装postgresql 注意:在flask中,操作数据库还是通过orm调用驱动来操作.sqlalchemy是python下的一款工业级的orm,比Django自带的orm要强大很多,至于什么类型 ...
- VIM简单操作
ngg就跳转到第n行行首,G就到结尾 0光标移到当前行行首 $光标移到当前行行末 fx搜索当前行中下一个出现x的地方 yy复制当前行 nyy复制当前行到n-1行 dd删除当前行 ndd删除当前行到n- ...
- 垃圾回收gc --翻译
原文在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management.基本保持了平译,并在一些地方做了概念解释.(转 ...
- Flow-based model
文章1: NICE: NON-LINEAR INDEPENDENT COMPONENTS ESTIMATION 文章2:Real-valued Non-Volume Preserving (Real ...
- filter和filter_by 的区别
- 使用jvisualvm远程监控tomcat(阿里云ECS)
写在前面: 使用jvisualvm远程监控tomcat(阿里云ECS),连接是报错:service:jmx:rmi:////jndi/rmi:IP:端口// 连接到 IP:端口,网上找了很多资料, ...
- IPC 进程间通信方式——管道
进程间通信概述 数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几兆字节之间 共享数据:多个进程想要操作共享数据,一个进程对共享数据的修改,别的进程应该立刻看到. 通知时间: ...
- c# 判断某个类是否实现某个接口
typeof(IFoo).IsAssignableFrom(bar.GetType()); typeof(IFoo).IsAssignableFrom(typeof(BarClass));
- redis-cli批量删除key
https://www.cnblogs.com/kiko2014551511/p/11531584.html redis-cli -a 密钥 keys "shiro*" | xar ...