Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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.

题目大意:给定两个等长的单词,各一个字典,每次只能变换一个字符得到中间词,此中间词必须在字典里存在,求从一个单词变换到另一个单词最短需要多少步。

解题思路:说实话这题一开始觉得不知道从哪儿下手,后来看了下tag是BFS,才有了思路,从字典里寻找与起始单词距离为1的所有单词,加入bfs队列,然后当队列非空,取出队列中的单词,查找在字典里的所有与之距离为1的单词,直到找到结束单词或者全部遍历完没有合法解,返回0;

Talk is cheap>>

    public int ladderLength(String start, String end, Set<String> dict) {
if (transInOne(start, end)) {
return 2;
}
Queue<String> queue = new ArrayDeque<>();
queue.add(start);
int length = 1;
while (!queue.isEmpty()) {
length++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String toSearch = queue.poll();
if (transInOne(toSearch, end)) {
return length;
}
for (Iterator<String> iterator = dict.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
if (transInOne(toSearch, next)) {
queue.offer(next);
iterator.remove();
}
}
}
}
return 0;
} private boolean transInOne(String start, String end) {
int i = 0;
int res = 0;
while (i < start.length()) {
if (start.charAt(i) != end.charAt(i)) {
res++;
if (res > 1)
return false;
}
i++;
}
return true;
}

Word Ladder——LeetCode的更多相关文章

  1. Word Ladder - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Word Ladder - LeetCode 注意点 每一个变化的字母都要在wordList中 解法 解法一:bfs.类似走迷宫,有26个方向(即26个字 ...

  2. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  3. [LeetCode] Word Ladder 词语阶梯

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

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

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

  5. LeetCode:Word Ladder I II

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

  6. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  7. [leetcode]Word Ladder II @ Python

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

  8. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  9. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

随机推荐

  1. spring mvc DispatcherServlet详解之前传---前端控制器架构

    前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端.前端控制器既可以使用Filter实现 ...

  2. 13、SQL Server 自定义函数

    SQL Server 自定义函数 在SQL Server中不仅可以使用系统函数(如:聚合函数,字符串函数,时间日期函数等)还可以根据需要自定义函数. 自定义函数分为标量值函数和表值函数. 其中,标量值 ...

  3. C# Wpf双向绑定实例

    Wpf中双向绑定处理需要两处 实例1: 1.前台Xaml中属性Binding 时Model指定 TwoWay <Grid> <Ellipse x:Name="ellipse ...

  4. OC - 21.CALayer核心要点及实例解析

    CALayer基础 CALayer是每一个UI控件的核心,一个UI控件之所以能显示可以说是CALayer的功劳 每一个UI控件默认都为自己创建一个CALayer对象,通过drawRect方法将内容绘制 ...

  5. iOS使用阿里云OSS对象存储 (SDK 2.1.1)

    最近项目中用到了阿里云OSS对象存储,用来存储APP中图片.音频等一些数据.但坑爹的阿里云居然在11月20日将SDK版本更新到了2.1.1,然而网上给出的教程都是1.*版本的(针对iOS),两个版本所 ...

  6. SQL window身份登陆 SQL server不能登陆

    用window方式登陆然后,在SQL Server Management Studio 中新建查询,执行下面代码一. ALTER LOGIN sa ENABLE GO ALTER LOGIN sa W ...

  7. nodejs原生模块简介

    一.Express框架 前面的章节已经介绍过了,可以使用npm来安装node.js模块.具体操作请参照以前写的nodejs概论. Express是一个nodejs的web开源框架,用于快速的搭建web ...

  8. JS格式化数字金额用逗号隔开保留两位小数

    JS格式化金额,正则方式修改. /** * 格式化金额 * @param {[type]} v [要转换的数字] * @param {[type]} len [小数点位数,默认2位] * @param ...

  9. 解决Mac上Android开发时adb连接不到手机问题

    今天在Mac OS上进行Android开发的时候,打开eclipse连接不到手机MX4问题 1. 插入手机打开 Terminal,输入 system_profiler  SPUSBDataType 2 ...

  10. python运维开发之第六天

    Python面向对象 python从设计之初就已经是一门面向对象的语言,在python中创建一个类和对象很容易. 面向对象简介:类(class),类变量,object(基类),实例变量,构造函数,封装 ...