LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个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 找单词变换梯的更多相关文章
- 127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [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 II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
随机推荐
- Http客户端再封装
Android系统上推荐的Http客户端从Apache变成[HttpURLConnection],主要理由包括 * 不过因为UrlConnection这组接口时间较早(Java 1.0), 接口的设计 ...
- Mathematics Base - 期望、方差、协方差、相关系数总结
参考:<深度学习500问> 期望 在概率论和统计学中,数学期望(或均值,亦简称期望)是试验中每次可能结果的概率乘以其结果的总和.它反映随机变量平均取值的大小. 线性运算: \(E(ax+ ...
- vue打包之后生成一个配置文件修改请求接口
问题描述: 在npm run build 生成dist后,url配置也被固定了,传到运行的前端服务器上后,假设某次,api服务器的ip修改了,改动只是更新下这个url,但是却需要回到前端源码,修改ur ...
- 有关UPDATE操作的一些想法
我们平常写代码的时候,无疑都会接触大量的数据CURD操作.第一反应是这太简单了,那么你在编写UPDATE操作的时候是怎样的逻辑呢?比较下面两段伪代码: code exp.1 $SQL = " ...
- hdu2586(lca模板 / tarjan离线 + RMQ在线)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 给出一棵 n 个节点的带边权的树, 有 m 个形如 x y 的询问, 要求输出所有 x, ...
- Java:基本语法
Java语言是由类和对象组成的,其对象和类又是由变量和方法组成,而方法,又包含了语句和表达式. 1. 变量 Java语言提供了两种变量:成员变量和局部变量 成员变量:是在方法体外的类中声明和定义的,可 ...
- jquery json实现省市级级联
java后台程序: import java.util.HashMap;import java.util.List;import java.util.Map; import javax.servlet. ...
- POJ1031 Fence
题目来源:http://poj.org/problem?id=1031 题目大意: 有一个光源位于(0,0)处,一个多边形的围墙.围墙是“全黑”的,不透光也不反射光.距光源r处的光强度为I0=k/r, ...
- Luogu P3546 [POI2012]PRE-Prefixuffix 神奇的递推+哈希
设$f[i]$表示切掉前$i$位和后$i$位后,即剩下$s[i+1]到s[n-i]$,的公共前后缀长度.此时我们发现,$f[i-1]$相对于$f[i]$少切了两个$char$,所以有$f[i-1]\l ...
- Linux定时清理30天前的Tomcat日志脚本
一.在tomcat的log路径下新建.sh脚本文件clean.sh,内容如下:#!/bin/bashlogs_path="/mnt/tomcat/apache-tomcat-8.5.23/l ...