Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

解题思路一:

对set进行逐个遍历,递归实现,JAVA实现如下:

	static public int ladderLength(String beginWord, String endWord,
Set<String> wordDict) {
int result = wordDict.size() + 2;
Set<String> set = new HashSet<String>(wordDict);
if (oneStep(beginWord, endWord))
return 2;
for (String s : wordDict) {
if (oneStep(beginWord, s)) {
set.remove(s);
int temp = ladderLength(s, endWord, set);
if (temp != 0)
result = Math.min(result, temp + 1);
set.add(s);
}
}
if (result == wordDict.size() + 2)
return 0;
return result;
} public static boolean oneStep(String s1, String s2) {
int res = 0;
for (int i = 0; i < s1.length(); i++)
if (s1.charAt(i) != s2.charAt(i))
res++;
return res == 1;
}

结果TLE

解题思路二:

发现直接遍历是行不通的,实际上如果使用了oneStep函数,不管怎么弄都会TLE的(貌似在C++中可以AC)。

本题的做法应该是采用图的BFS来做,同时oneStep的匹配也比较有意思,JAVA实现如下:

static public int ladderLength(String start, String end, Set<String> dict) {
HashMap<String, Integer> disMap = new HashMap<String, Integer>();
LinkedList<String> queue = new LinkedList<String>();
queue.add(start);
disMap.put(start, 1);
while (!queue.isEmpty()) {
String word = queue.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 (end.equals(nextWord))
return disMap.get(word) + 1;
if (dict.contains(nextWord) && !disMap.containsKey(nextWord)) {
disMap.put(nextWord, disMap.get(word) + 1);
queue.add(nextWord);
}
}
}
}
return 0;
}

Java for LeetCode 127 Word Ladder的更多相关文章

  1. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  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、126. Word Ladder II

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

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

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

  5. Leetcode#127 Word Ladder

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

  6. leetcode 127. Word Ladder ----- java

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

  7. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

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

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

  9. [LeetCode] 127. Word Ladder _Medium tag: BFS

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

随机推荐

  1. 鼠标悬浮弹出标题制作JQuery

    今天给客户制作的网站里面加个效果,当鼠标在列表图片之外时,标题不显示,当鼠标悬浮在图片之上时,标题从底部弹出. 效果图如下: 鼠标悬浮前: 鼠标悬浮后: html代码如下: <ul class= ...

  2. linux中shell脚本中系统预先定义的变量

    $0:脚本名称: $*:所有参数: $$:当前进程或者脚本的PID号: $!:后台运行的最后一个进程的PID号: $?:用于返回上一个命令是否成功.成功0,否则为非零: $#:参数个数: $@:所有参 ...

  3. C 标准库 - <assert.h>

    C 标准库 - <assert.h> 简介 C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息. 已定义的宏 a ...

  4. SqlServer 垂直分表

    当单表数据太多时.我们能够水平划分,參考 SqlServer 分区视图实现水平分表 ,水平划分能够提高表的一些性能. 而 垂直分表 则相对非常少见到和用到,由于这可能是数据库设计上的问题了.假设数据库 ...

  5. props default 数组(Array)/对象(Object)的默认值应当由一个工厂函数返回

    1.场景: Object: <!-- 步骤 --> <template> <div> <div class="m-cell"> &l ...

  6. 【转载】ORM的概念, ORM到底是什么

    一.ORM简介         对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.简单的说,ORM是通过使 ...

  7. Matlab 绘图全方位分析及源码

    Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...

  8. 高阶函数:sorted

    排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的 ...

  9. 封装CLLocationManager定位获取经纬度

    创建调用方法,在.h文件里 #import <Foundation/Foundation.h> @interface RMMapLocation : NSObject { void (^s ...

  10. dede频道标签channel和频道内容标签channelartlist的调用栏目名的不同方式,如果错误使用标签会发生错误

    频道标签 [field:typename/] 频道内容标签 {dede:field name='typename'/}