LeetCode_Word Ladder
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的更多相关文章
- [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 ...
随机推荐
- #ifdef __cplusplus extern "C" {代码} 倒底是什么意思?
时常在cpp的代码之中看到这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } # ...
- 自制单片机之五……LCD12864的驱动
LCD12864的驱动LCD12864在市面上主要分为两种,一种是采用st7920控制器的,它一般带有中文字库字模,价格略高一点.另一种是采用KS0108控制器,它只是点阵模式,不带字库.很可惜,我的 ...
- Powershell 定义文本
使用引号可以定义字符串,如果想让自己定义的字符串原样输出,可以使用单引号. 1 2 $text='$fei $(tai) $env:windir 飞苔博客 (20+2012)' $text 输出: $ ...
- HP的笔记本经常蓝屏崩溃 -------athr.sys
因为windows 7才新装不久,没有时间下载配置什么符号表,直接临时下载了WinDbg分析下Dump文件, Probably caused by : athr.sys ( athr+428a5 ) ...
- bzoj1145
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1145 神题...... 定义f(abcd)为高度排名为abcd的个数,例如闪电的个数为f(13 ...
- VS项目如何添加到svn
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.这里就讲一下VS2010如何将项目导入SVN版本控制. 工具 ...
- list 操作
animals = ["aardvark", "badger", "duck", "emu", "fennec ...
- 手游与App测试如何快速转型? —— 过来人科普手游与App测试四大区别
随着智能设备的普及和移动互联网的兴起,各家互联网巨头纷纷在往移动端布局和转型,同时初创的移动互联网公司也都盯着这个市场希望分一杯羹.在这个大环境下,互联网的重心已经慢慢从Web端转向了移动端,而移动端 ...
- Vericant维立克 | 氪加
Vericant维立克 | 氪加 Vericant维立克
- 浅谈c语言代码段 数据段 bss段
代码段.数据段.bss段 (1)编译器在编译程序的时候,将程序中的所有的元素分成了一些组成部分,各部分构成一个段,所以说段是可执行程序的组成部分. (2)代码段:代码段就是程序中的可执行部分,直观理解 ...