题意:给出两个单词,以及一个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. Socket通信常见问题

    1.检查服务器防火墙入站规则,是否允许对应端口通过.如果是云服务器,还需要通过对应账户去设置安全规则 2.服务端监听或绑定端口时,最好使用IPAddress.Any监听所有网口的改端口,创建socke ...

  2. Python中读取,显示,保存图片的方法

    一 opencv import cv2 as cv # load img = cv.imread(imagepath) # shape=(height, width, channel) h,w,c = ...

  3. html的Vue.js框架概述

    前端的三大框架: Augular.js          由Google的研发团队最先写出 React.js            由facebook的团队继Augular.js之后写出 Vue.js ...

  4. express-http-proxy 的基础使用

    const app = express() app.use(matchPath, proxy(serverAddress, { proxyReqPathResolver: function(req) ...

  5. POJ1027 The Same Game

    题目来源:http://poj.org/problem?id=1027 题目大意: 题目说的就是现在蛮流行的手机小游戏popstar,求用贪心方法能得到多少分. 小球有三种颜色:R/G/B.横向.纵向 ...

  6. DSL与GPL

    一.DSL 与 GPL DSL(Domain-Specified Language 领域特定语言),而与 DSL 相对的就是 GPL,最常见的 DSL 包括 Regex 以及 HTML & C ...

  7. SocLib能耗评估

    相关论文链接: https://ac.els-cdn.com/S0167926015000231/1-s2.0-S0167926015000231-main.pdf?_tid=4a67f1a4-b21 ...

  8. datagrid 里面的formatter方法

    A.{field:'station_staus',title:'工位状态',width:250,align:'center',formatter: function(value,row,index){ ...

  9. 完美解决百度地图MarkerClusterer 移动地图时,Marker 的Label 丢失的问题

    这篇文章来自http://www.cnblogs.com/jicheng1014   不好意思,那些rss 站太生猛了. 先吐槽一下百度地图的开发者,其实这个问题我绝对不是第一个遇到的人 很多人把这个 ...

  10. Spring Cloud下使用Feign Form实现微服务之间的文件上传

    背景 ​ Spring Cloud现在已经被越来越多的公司采用了,微服务架构比传统意义上的单服务架构从复杂度上多了很多,出现了很多复杂的场景.比如,我们的产品是个app,支持第三方登录功能,在手机端调 ...