题意:给出两个单词,以及一个set集合,当中是很多的单词。unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了。要求找出一个从start到end这两个单词的变换序列。从start开始,每次可以变一个字母,且所变之后的单词必须在set中,最后要求变成end,问经过了多少个中间变换?注意要加多2次(start和end也要算),这是规定。

思路:广度搜索,以start为树根,一层一层扩展,直到找到end,返回数的深度即可。步骤是这样的,先画出树根start,遍历set,所有能被start够经过1个字母的变换得到的,取出来(要删掉)做为第二层,也就是作为树根的孩子。接着以第二层的每个元素为起点,继续遍历set中的元素,直到搜到end,计算深度返回。

注:千辛万苦用g++的4.8.1版才能编译测试,网传可以用对当前单词的每个字母用a~z每个字母代替一次,再在set中查找出来,这个方法感觉看不出优势,n个单词,单词长为k,最差大概n*k*26次。下面这个是n*k。很累没有详细验证,大概就这样吧。搞了3天,才知道被那个for括号中第二个式子给玩坏了,它每次循环都会检查,也就是更新界限。

 class Solution {
public:
bool mat(string &lef,const string &rig) /*返回两个字符串是否匹配(允许一个字母不匹配)*/
{
int count=;
for(int i=; i<lef.size(); i++)
{
if(lef[i]!=rig[i])
{
count++;
if(count>=) return false;
}
}
return true; //不可能出现相等的,即count=0的情况
} int ladderLength(string start, string end, unordered_set<string> &dict) {
if( start.empty() || end.empty() || start==end || start.length() != end.length() ) return ;
if( mat(start,end) ) return ; //只有一个不匹配
if( dict.find(end) == dict.end() ) dict.insert(end);//end必须在set中
if( dict.find(start)!=dict.end() ) dict.erase(start); //start必须不在setzhong
unordered_set<string>::iterator dist = dict.find(end); //终点指针
unordered_set<string>::iterator it = dict.begin();
queue<string> que;
que.push(start); //起点先进队
int count=;
while(!que.empty())
{
count++;
int q=que.size(); //注意这里,不能将que.size()放在下一行的括号中代替q,它每次循环都检查一遍
for(int i=; i<q; i++) //此for扫描同一层的元素
{
it = dict.begin();
while( it!=dict.end() ) //搜dict中每个元素
{
if( mat( que.front(), *it) )
{
if( it == dist ) return count; //找到终点end
que.push(*it);
it = dict.erase(it); //在集合中删去
}
else it++;
}
que.pop();
}
}
return ;
}
};

word ladder

LeetCode Word Ladder 找单词变换梯的更多相关文章

  1. 127. Word Ladder(单词变换 广度优先)

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. LeetCode:Word Ladder I II

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

  3. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  4. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  5. [LeetCode] Word Ladder 词语阶梯

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

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

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

  7. LeetCode: Word Ladder II 解题报告

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

  8. LeetCode: Word Ladder II [127]

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

  9. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

随机推荐

  1. IOS开发学习 碎片S

    非常感谢提供一下内容的人和组织! 字符串编码:http://www.cnblogs.com/KevinYang/archive/2010/06/18/1760597.html Foundation框架 ...

  2. asp.net core 邮件发送

    由于core不带smpt 所以借助MimeKit 以163邮箱为例 var message = new MimeMessage ();message.From.Add (new MailboxAddr ...

  3. ASP.NET MVC 小牛之旅1:何谓MVC

    在学习ASP.NET MVC之前首先了解什么 是MVC ? MVC不是一种语言,严格来说也不算一个技术,而是开发时所使用的一种架构(框架),它就像是一种开发观念,或是一个设计样式. MVC让软件开发的 ...

  4. 如何在MySQL中设置外键约束

    引用:http://blog.sina.com.cn/s/blog_53729e4601011wja.html MySql外键设置详解   (1) 外键的使用: 外键的作用,主要有两个:    一个是 ...

  5. 开发php接口注意点

    1.制定规范 开发前一定要定好一个规范,比如要定好数据返回的通用参数和格式.关于数据格式,用的比较多的有xml和json,我建议用json,因为json比xml的好处更多. 2.精简的返回数据 接口数 ...

  6. [Xcode 实际操作]四、常用控件-(6)UISwitch开关控件的使用

    目录:[Swift]Xcode实际操作 本文将演示开关控件的基本用法. 开关控件有两个互斥的选项,它是用来打开或关闭选项的控件. 在项目导航区,打开视图控制器的代码文件[ViewController. ...

  7. module.exports 和 export default

    CommonJS模块规范和ES6模块规范完全是两种不同的概念 CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一 ...

  8. java 集合解析

    Set集合,放的元素不能重复,请问它的判断重不重复是怎么实现的? 比如说:ArrayList 和 Vector 是用数组的方式存储的Set里的 hashSet 和TreeSet是用什么方式存储的?怎么 ...

  9. JS异步解决方案之概念理解-----------阻塞和非阻塞,同步和异步,并发和并行,单线程和多线程

    首先记住一句话,JS是单线程的. 单线程意味着什么?单线程意味着 它不能依靠自己实现异步. JS实现的异步,往往都是靠 浏览器.Node 的机制(事件驱动.回调)实现的. 下面让我这个单身狗 以谈恋爱 ...

  10. 【JavaScript权威指南】——逻辑与(&&)

    三种用法总结: 1.布尔值计算: [成员]={false,true} 2.“真值”,“假值”计算: [假值]={false,null,undefined,0,-0,NaN,""} ...