题意:给出两个单词,以及一个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. JDK1.8的安装与卸载

    Java的下载地址: https://www.java.com/zh_CN/download/ 下载完成后打开进入界面: 点击下一步, 上面也无需更改,也可自定义设置安装路径, 再次确认安装路径,点击 ...

  2. Codeforces Round #523 (Div. 2)D(二分,多重集)

    #include<bits/stdc++.h>using namespace std;const long long N=1e5+5;const long long MOD=1e9+7;l ...

  3. (PHP)redis Hash(哈希)操作

    /** * * Hash操作 * 哈希操作 * 可理解为数据库操作 * */ //为user表中的字段赋值.成功返回1,失败返回0.若user表不存在会先创建表再赋值,若字段已存在会覆盖旧值. $re ...

  4. 通过jQuery实现AJAX

    通过jQuery实现AJAX > 使用get和getJSON都会有缓存问题,并且使用get方法不能传送较多的数据. 问题: 在IE浏览器中,get请求使用ajax存在缓存问题,会使用上一次请求的 ...

  5. PHP与thinkphp中var_dump()打印数组显示不全问题

    在我们进行php开发的时候,经常会使用var_dump()函数进行数组的打印,以方便我们程序的调试,而有时候我们在进行多维数组打印的时候会发现多维数组打印不全,有些地方被…代替,这就是我们php配置的 ...

  6. python实现王者荣耀英图片收集

    一个python写的小爬虫项目,爬虫相关的很容易写,关键是怎么找到爬取图片的位置. 图片位置分析 hero_list_url = 'http://pvp.qq.com/web201605/js/her ...

  7. 从sql中获取表名

    <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser ...

  8. Seven-segment Display 贪心选择,快速判断能否有解

    https://csacademy.com/contest/round-39/task/seven-segment-display/ 可以知道,只有1是无解 而且肯定是选出来的位数约小越好. 位数 = ...

  9. Java面向对象_常用类库api

    StringBuffer 例: public class StringBufferDemo { /** * @param args */ public static void main(String[ ...

  10. (转)不看绝对后悔的Linux三剑客之grep实战精讲

    不看绝对后悔的Linux三剑客之grep实战精讲 原文:http://blog.51cto.com/hujiangtao/1923675 https://www.cnblogs.com/peida/a ...