给出两个单词(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. UVA - 11019 Matrix Matcher hash+KMP

    题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...

  2. UVA 10887 Concatenation of Languages 字符串hash

    题目链接:传送门 题意: 给你两个集合A,B,任意组合成新的集合C(去重) 问你最后C集合大小 题解: 暴力 组成的新串hash起来 #include<bits/stdc++.h> usi ...

  3. REST RPC HTTP vs 高性能二进制协议 序列化和通信协议

    edisonchou https://mp.weixin.qq.com/s/-XZXqXawR-NxJMPCeiNsmg .NET Core微服务之服务间的调用方式(REST and RPC) Edi ...

  4. 编写自定义PE结构的程序(如何手写一个PE,高级编译器都是编译好的PE头部,例如MASM,TASM等,NASM,FASM是低级编译器.可以自定义结构)

    正在学PE结构...感谢个位大哥的文章和资料...这里先说声谢谢 一般高级编译器都是编译好的PE头部,例如MASM,TASM等一直都说NASM,FASM是低级编译器.可以自定义结构但是苦于无人发布相关 ...

  5. [RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】

    本文转载自:http://m.blog.csdn.net/kris_fei/article/details/69553422 Platform: ROCKCHIPOS: Android 6.0Kern ...

  6. hdu acm 2844 Coins 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目意思:有A1,A2,...,An 这 n 种面值的钱,分别对应的数量是C1,C2,...,C ...

  7. WAS:启动服务后,server一会挂起。

    有个WAS集成,其中一台因为linux系统异常需要重新安装,于是服务器停了:一会现场提报ERP系统访问不了. 查看了另外一个server后台日志,有一批webcontainer进程挂起,明显服务死了. ...

  8. BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP

    BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP Description Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的 ...

  9. C#使用SendMessage发送组合键

    有时需要出发菜单功能,例如发送ALT + F打开应用程序的文件菜单,如何使用SendMessage实现呢? 使用用spy++截取的ALT+F的消息内容(如何使用spy++,请熟悉的高手指点下,我使用s ...

  10. bzoj 2836 魔法树 —— 树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树链剖分裸题: 写码五分钟,调码两小时,RE不断,狂交二十五遍,终于找到一处小细节—— ...