给出两个单词(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. 初学unity 3D 遇到的一个问题--预制体选项没有找到。

    没有找到预制体这个选项. 我的工程如下:

  2. pyspark mongodb yarn

    from pyspark.sql import SparkSession my_spark = SparkSession \ .builder \ .appName("myApp" ...

  3. HTML canvas

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  4. mongodb的安装、配置、常见问题

    一.MongoDB下载 mongodb可以在官网找到下载链接,找到合适的版本进行下载.下载地址->https://www.mongodb.com/download-center?jmp=nav# ...

  5. JS中prototype,js原型扩展

    作者:轩脉刃(yjf512)出处:(http://www.cnblogs.com/yjf512/)版权声明:本文的版权归作者与博客园共有.欢迎转载阅读,转载时须注明本文的详细链接. 原文 http:/ ...

  6. QT实现FTP服务器(二)

    QClientThread类的实现: #include "QClientThread.h" #include <QDebug> /******************* ...

  7. HDU1964 Pipes —— 插头DP

    题目链接:https://vjudge.net/problem/HDU-1964 Pipes Time Limit: 5000/1000 MS (Java/Others)    Memory Limi ...

  8. DEDECMS织梦自定义表单中必填项、电话邮箱过滤以及验证码规则

    织梦自定义表单必填项规则--->(wwwshu-acca.com网站表单) 1. 在plus/diy.php 的第 40行下加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  9. 网络编程、三要素、Socket通信、UDP传输、TCP协议、服务端(二十五)

    1.网络编程概述 * A:计算机网络 * 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传 ...

  10. WPF之Binding深入探讨 转载:http://blog.csdn.net/fwj380891124/article/details/8107646

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...