Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. 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 未完成的更多相关文章

  1. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  2. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  4. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  5. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  6. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. LeetCode127:Word Ladder II

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

随机推荐

  1. 迭代器 iterator(二): 用iterator遍历arraylist

           迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些 ...

  2. iOS加载程序视图的方式

    The UIViewController class provides built-in support for loading a view controller's views whenever ...

  3. IOS 杂笔-4(属性与成员变量的区别)

    属性可以用点语法,比如self.xxx,在外部调用也同样可以someClass.xxx. 属性实际上是对一组set和get方法的简单封装(oc的get方法没有get前缀),同样会自动生成一个私有的成员 ...

  4. windows log 打印语句

    1.格式化字符串(Writes formatted data to the specified string) wchar_t szMessage[260]; PWSTR pszFunction = ...

  5. 记录JVM垃圾回收算法

    垃圾回收算法可以分为三类,都基于标记-清除(复制)算法: Serial算法(单线程) 并行算法 并发算法 JVM会根据机器的硬件配置对每个内存代选择适合的回收算法,比如,如果机器多于1个核,会对年轻代 ...

  6. python 连接 db2

    export IBM_DB_DIR=/home/db2inst1/sqllib export IBM_DB_LIB=/home/db2inst1/sqllib/lib http://programmi ...

  7. android中的事件传递和处理机制

    一直以来,都被android中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...

  8. RESTful API的设计与开发

    自己做过关于RESTful API的培训,下载

  9. C# 日志框架的添加

    .NET中 记录日志的比较好的主要是Log4Net和Enterprise Library的Logging 复杂一点的还可以实现自动化Log日志 教程 首先是第二种方式 1.需要添加以下几个DLL  下 ...

  10. redis的简单安装配置

    一.简介 Redis是一种高级key-value数据库,数据可以持久化,支持的数据类型很丰富,有字符串,哈希,链表,集合和有序集合5种数据类型 Redis支持在服务器端计算集合的并,交和补集(diff ...