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 ...
随机推荐
- MCS-51单片机实用子程序库
目前已有若干版本的子程序库公开发表,它们各有特色.本程序库中的开平方算法为快速逼近算法,它能达到牛顿迭代法同样的精度,而速度加快二十倍左右,超过双字节定点除法的速度. 本子程序库对<单片机应用程 ...
- 短信,微信API(还能发邮件,短信,IM聊天)
1.用于微信公众平台的Js API(WeixinApi) Github地址:https://github.com/zxlie/WeixinApi http://www.baidufe.com/item ...
- 【Xamarin开发IOS-IOS生命周期】
iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受 ...
- android 通过shape设置圆形按钮
Android中常常使用shape来定义控件的一些显示属性来美化UI: shape的常用属性有: (1)solid:填充,设置填充的颜色: (2)stroke:描边,设置边界的宽度.颜色等: (3)c ...
- Hadoop开发遇到的问题之reduce卡住
遇到的问题描述:在hadoop上面执行程序,程序运行之后能够正常执行.一切似乎都是正常的,然而过了一段时间之后程序便开始阻塞直到程序超时退出(如下). 14/08/19 21:17:51 INFO m ...
- osip及eXosip的编译方法
osip及eXosip的编译方法 在最新版本的osip2.exosip2中不支持在VC6.0下编译osip.exosip协议栈的方法 说明: 1.以下文章中的osip版本为3.1.0 2.eXosip ...
- hdu5035:概率论推公式
题目大意: 你要去邮局发一个包裹,有n个窗口,每个都有人,每一个窗口完成一次服务的时间 ti 的分布符合几何分布:ki*e^(-ki*t) 每个窗口当前服务已经进行了ci时间 你会去第一个完成当前服务 ...
- Java中的enum
package com.st.java; /** * ENUM枚举类型的使用 * @author Administrator * 2016年04月10日 */ public enum MoneyTyp ...
- mac复制粘贴剪切
win下复制粘贴剪切: Ctrl+C,Ctrl+V,Ctrl+X; mac下lion之后已经有了一直让win用户吐槽的剪切功能: 复制粘贴剪切:Command+C,Command+V,Command+ ...
- centos6.5配置无线网络
由于安装的是服务器版,所以开机无法连接网络,以下这些情况都是针对驱动已经安装OK.按步骤操作如下(以下操作默认都是在超级管理员权限下进行): 1.测试电脑是否安装wpa_supplicant,测试方法 ...