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 ...
随机推荐
- SSM整理笔记1——SSM网站初步功能设计
前言 因为公司里一直用Hibernate,但是现在Mybatis是趋势,所以搭建一个Mybatis的网站框架,目的是:1摸清其功能特点,2为以后的项目增加框架选择(以前只用hibernate或者Spr ...
- java中方法中声明三个点“...”作用
public class Test { public static void main(String[] args) { String str[] = {"s"," ...
- 使用双引擎,让kbmmw 的客户端访问更方便(既给浏览器做服务,也给桌面程序做服务)
前面我们一直都讲了如何使用kbmmw smarthttpservice 给客户端提供REST 服务.主要都是返回给 浏览器访问的,如果我们使用delphi 开发桌面应用,如何使用这些服务呢?其实一切 ...
- extjs 4 checkboxgroup Panel的简单用法
Ext.require([ 'Ext.tree.*', 'Ext.data.*', 'Ext.window.MessageBox', 'Ext.tip.*' ]); Ext.onReady(funct ...
- 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...
- ES6 一些新特性的总结
一.箭头函数 ES6中新增了一个箭头函数 ()=>,箭头函数通俗点讲就是匿名函数.箭头函数还有不同点在于改变函数中this,和js中的.bind 的方法差不多,继承后指向的不是最新的函数, ...
- BootLoader与Linux内核的参数传递【转】
本文转载自:http://blog.sina.com.cn/s/blog_476d8cf30100rttx.html 在嵌入式系统中,BootLoader 是用来初始化硬件,加载内核,传递参数.因为嵌 ...
- 一步一步学Silverlight 2系列(26):基本图形
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 关于O_DIRECT的那些事儿
很久之前落下的坑,一直没怎么记录-.- 一般地,如果在Linux内核中读写一个文件,其IO流程都需要经过Kernel内的page cache层次,如果程序员若想要使用自己开发的缓存系统,那么就可以在打 ...