127 Word Ladder 单词接龙
给出两个单词(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 单词接龙的更多相关文章
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 127. Word Ladder(M)
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- 127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- HDU 6068 Classic Quotation KMP+DP
Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...
- Kubernetes实战阅读笔记--2、架构和部署
安装Kubernetes “本书准备了4台虚拟机(CentOS 7.0系统)用于部署Kubernetes运行环境,包括一个Etcd.一个Kubernetes Master和三个Kubernetes N ...
- Designing a RESTful API with Python and Flask 201
rest服务器的搭建 - CSDN博客 http://blog.csdn.net/zhanghaotian2011/article/details/8760794 REST的架构设计 REST(Rep ...
- 获取Android设备无线和以太网MAC地址
package com.raycloud.wolf.blogformac; import android.net.wifi.WifiManager; import android.support.v7 ...
- HDU2089 不要62 —— 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) M ...
- MYSQL学习拓展一:MySQL 存储过程之游标的使用!
一.MySQL游标的概念 游标介绍: MySQL的游标(cursor)是一个重要的概念,通过查找资料与自己的理解,主要得出以下几点关于自己的理解. 有数据缓冲的思想:游标的设计是一种数据缓冲区的思想, ...
- React引入,运行
1.引入 <script src="https://cdn.bootcss.com/react/15.5.4/react.min.js"></script> ...
- 【Java】通过移除空行和注释来压缩 JavaScript 代码
1. [代码]JavaScriptCompressor.java/** * This file is part of the Echo Web Application Framework (herei ...
- (转)Linux下 SVN客户端安装
原地址:http://rtxbc.iteye.com/blog/860092 今天有现场程序连svn服务器一直有异常,于是在现场linux下安装svn client来直接测试,看问题原因: 一:安装s ...
- I.MX6 system.img unpack repack
/************************************************************************* * I.MX6 system.img unpack ...