Word Ladder 未完成
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, 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
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
每次只能改变一个字符,求最短路径。
开始想法为列出二维矩阵,找出变化一次,变化两次,知道变化为end,从而求最短路径。然而发现需要内存过多,同时超时。
改为采用BFS,这样首先找到的肯定是最短路径。但是同样超时。看到网上都是用java实现的,不知道是什么问题。
class Solution {
private:
int isOneDiff(string beginWord, string endWord)
{
int n=beginWord.size();
int m=endWord.size();
if(n!=m) return -;
int count=;
for(int i=;i<n;i++)
{
if(beginWord[i]!=endWord[i])
count++; }
if(count>) return -;
return count;
}
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
int n=wordDict.size();
if(beginWord.empty()||endWord.empty()||n<||beginWord.size()!=endWord.size()||isOneDiff(beginWord,endWord)==)
return ;
if(isOneDiff(beginWord,endWord)==)
return ;
if((wordDict.find(beginWord)!=wordDict.end())&&(wordDict.find(endWord)!=wordDict.end())&&(n==))
return ;
queue<string> q;
map<string,int> wordmap;
int wordlength=beginWord.size();
int count=;
q.push(beginWord);
wordmap.insert(pair<string,int>(beginWord,count));
while(!q.empty())
{
string tmpword=q.front();
count=wordmap[tmpword];
q.pop();
for(int i=;i<wordlength;i++)
{ for(char j='a';j<='z';j++)
{
if(j==tmpword[i]) continue;
tmpword[i]=j;
if(tmpword==endWord) return count+;
if(wordDict.find(tmpword)!=wordDict.end())
{
q.push(tmpword);
wordmap.insert(pair<string,int>(tmpword,count+));
}
}
}
} return ;
}
};
Word Ladder 未完成的更多相关文章
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- 【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 ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
随机推荐
- 关于Kb/s,KB/s的一些知识
我们常见的有KB/s和Kb/s两种 1,Kb/s也就是Kbps.这里面小写的b是bit(比特)的缩写,是位的意思.一个位就是二进制的0或者1.一般代表传输单位,p就是/号,s是秒.bps就是b/s=比 ...
- 协议(Protocol)---实例
协议:声明一些必须实现的方法和选择实现的方法,用来声明一些方法,即一个Protocol是由一系列的方法声明组成的. 建立协议文件步骤:将鼠标放到文件列表处,利用快捷键 command +N 健,得到如 ...
- 读书笔记-Autonomous Intelligent Vehicles(一)
Autonomous intelligent vehicles have to finish the basic procedures: perceiving and modeling environ ...
- iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
需求: 问题: 源码百度云下载链接: http://pan.baidu.com/s/1o7r4hCm 密码: 8atd 其他学习链接:http://www.jianshu.com/p/90d6cd35 ...
- mybatis3.3 + struts2.3.24 + mysql5.1.22开发环境搭建及相关说明
一.新建Web工程,并在lib目录下添加jar包 主要jar包:struts2相关包,mybatis3.3相关包,mysql-connector-java-5.1.22-bin.jar, gson-2 ...
- Effective Java 35 Prefer annotations to naming patterns
Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...
- MySQL 如何修改字符集 utf8 改为 utf8mb4
在实行sql server 向 mysql 迁移数据时,报错: Incorrect string value: '\xF0\x9F\x98\x8A' 原因是mysql 采用的是 utf8 的字符集,而 ...
- 【windows环境下】RabbitMq的安装和监控插件安装
RabbitMq的安装: RabbitMQ是基于Erlang的,所以必须先配置Erlang环境. 下载Erlang,地址:http://www.erlang.org/download/otp_win3 ...
- STM32启动文件选择说明
图1. STM32F10xxx标准外设库体系结构先说这个问题,大家都知道,我们在选择使用哪些外围的的时候,是去更改从官方模版中拷贝过来的stm32f10x_conf.h文件的27-48行,把我们要用的 ...
- splice()函数,'SPLICE_F_MOVE' 'SPLICE_F_NONBLOCK' 'SPLICE_F_MORE' undeclared
1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE' undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclare ...