Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, 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.

  DFS 小数据AC:

class Solution {
public:
bool check(const string & a, const string &b)
{
int num = ;
if(a.size() != b.size()) return false;
for(int i = ; i< a.size() ; i++)
{
if(a[i] != b[i])
num++;
}
return num == ;
}
void DFS(const string &start, const string &end, unordered_set<string> &dict, vector<bool> &flag, int nums){ if(start == end ){
res = res > nums ? nums : res;
return ;
}
int i;auto it = dict.begin();
for( i= ; it != dict.end(); it++,i++)
if(flag[i] == false && check(start,*it))
{
flag[i] = true;
DFS(*it,end,dict, flag, nums+);
flag[i] = false;
} }
int ladderLength(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res = dict.size() + ;
vector<bool> flag(dict.size(), false);
DFS(start, end, dict, flag, );
if(res == dict.size() + ) return ;
return res + ;
}
private :
int res;
};

BFS: 过大数据

class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(start.size() != end.size()) return ;
if(dict.size() == ) return ; queue<string> myqueue, myqueueT;
myqueue.push(start);
int depth = ; while(!myqueue.empty()){
depth++;
while(!myqueue.empty()){
string str = myqueue.front();
myqueue.pop();
for(int i = ; i < str.size() ; i++){
char temp = str[i] ;
for(char c = 'a'; c <= 'z' ;c++){
if(c == temp) continue;
str[i] = c;
if(str == end) return depth;
auto it = dict.find(str) ;
if(it != dict.end() ){
myqueueT.push(str);
dict.erase(it);
}
}
str[i] = temp;
}
}
myqueue.swap( myqueueT);
}
//don't find
return ;
}
};

LeetCode_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. Codeforces 573B Bear and Blocks

    http://codeforces.com/problemset/problem/573/B  题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...

  2. 必须用C模拟OS?

    ASM基本必要,至于高级语言就很难说了.去osdev wiki上一翻一堆各种语言实现的玩意. 一个模拟OS其实不太容易完整搭出来,反倒是直接构造内核的后顾之忧少(如果还有真的想在SIGALRM里耍什么 ...

  3. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  4. Android Animations简介

    一 .Animations简介 Animations提供了一系列的动画效果,这些效果可以应用于绝大多数的控件: 二.Animations的分类 第一类:TweenedAnimations,该类Anim ...

  5. windows puppet manifests 文件维护

    初级 puppet windows agent实现简单的msi格式安装包安装及bat文件创建;

  6. Java单元测试工具:JUnit4(一)(二)(三)(四)

    Java单元测试工具:JUnit4(一)--概述及简单例子 Java单元测试工具:JUnit4(二)--JUnit使用详解 Java单元测试工具:JUnit4(三)--JUnit详解之运行流程及常用注 ...

  7. (转)iOS7界面设计规范(10) - UI基础 - 文字排版与配色

    明天就是周四了.貌似前几天还在恨周一呢.话说今天几乎开了一整天的会,正经事情没做多少:这种感觉比一整天从早到晚12个小时的忙碌于一件事情还要让人感到疲惫的对叭?那今天的iOS7设计规范更新又是一篇很简 ...

  8. Android软键盘弹出时布局问题

    最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...

  9. LG 2.2.1 P350安卓系统刷机,问题总结,希望对需要的朋友有助

    手机误删软件导致短信,键盘等无声音提醒 我的手机前几天被我误删了一个软件,导致电话接不了,别人打电话的时候,老提示我在通话中,但是我可以在通话中看到对方的打电话记录.短信,键盘,USB连接,等等都没有 ...

  10. (转)SQL语句中的N'xxxx'是什么意思

    SQL语句中的N'xxxx'是什么意思 我们在一些sql存储过程,触发器等中经常会见到类似 N'xxxx' 是什么意思? 例如:if exists (select * from dbo.sysobje ...