给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则:
    每次只能改变一个字母。
    变换过程中的中间单词必须在字典中出现。
例如,给出:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
一个最短的变换序列是: "hit" -> "hot" -> "dot" -> "dog" -> "cog",
返回长度 5。
注意:
    如果没有这样的转换序列,则返回0。
    所有单词具有相同的长度。
    所有单词只包含小写字母字符。
    您可能会认为单词列表中没有重复项。
    你可能会认为 beginWord 和 endWord 是非空的并且不一样。
详见:https://leetcode.com/problems/word-ladder/description/

Java实现:

class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
HashMap<String, Integer> m = new HashMap<String, Integer>();
LinkedList<String> que = new LinkedList<String>();
que.add(beginWord);
m.put(beginWord, 1);
while (!que.isEmpty()) {
String word = que.poll();
for (int i = 0; i < word.length(); i++) {
for (char ch = 'a'; ch <= 'z'; ch++) {
StringBuilder sb = new StringBuilder(word);
sb.setCharAt(i, ch);
String nextWord = sb.toString();
if (endWord.equals(nextWord)){
return m.get(word) + 1;
}
if (wordList.contains(nextWord) && !m.containsKey(nextWord)) {
m.put(nextWord, m.get(word) + 1);
que.add(nextWord);
}
}
}
}
return 0;
}
}

参考:https://www.cnblogs.com/springfor/p/3893499.html

https://www.cnblogs.com/tonyluis/p/4532839.html

127 Word Ladder 单词接龙的更多相关文章

  1. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  2. [leetcode]127. Word Ladder单词接龙

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

  3. [LeetCode] 127. Word Ladder 单词阶梯

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

  4. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  5. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

  6. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. 127 Word Ladder

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

  8. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

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

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

随机推荐

  1. 序列化模块 json pickel shelve

    一.json  模块 1.定义 将字典.列表等内容转换成字符串的过程就是序列化. 操作的数据类型有限,但是可以支持所有编程语言操作. 2.为什么要有序列化? 1.以某种存储形式使自定义对象持久化. 2 ...

  2. CSS3 (二)

    translate() 方法 通过 translate() 方法,元素从其当前位置移动,根据给定的 left(x 坐标) 和 top(y 坐标) 位置参数: 示例: div { transform: ...

  3. ubuntu gcc低版本过低引起错误

    错误内容: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 您可能需要运行“apt-get -f install”来纠正下列错误:下列软件包有未满足的依赖关系: ...

  4. SQL 和 NoSQL 比较

    定义: SQL (Structured Query Language) 数据库,指关系型数据库.主要代表:SQL Server,Oracle,MySQL(开源),PostgreSQL(开源). NoS ...

  5. css3的渐变效果

    1.css3 渐变的属性 例子: #grad { background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */ back ...

  6. java中Math常用方法

    public class Demo{ public static void main(String args[]){ /** *Math.sqrt()//计算平方根 *Math.cbrt()//计算立 ...

  7. hdu2063 二分图(基础题)

    这个题目适合刚刚接触二分图的同学做哦: 给一个题目链接 点击打开链接. 题目大意,有K个男女匹配方式, 输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数.0 ...

  8. Unity5.6打包问题

    将unity切换到5.6版本后打Android包时,提示android sdk tools version低于compile version,于是更新了android-sdk(下载了Android-S ...

  9. 书写优雅的shell脚本(三) - shell中exec解析

    参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...

  10. ubuntu IP 扫描

    /******************************************************************************* * ubuntu IP 扫描 * 说明 ...