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. Docker 容器网络

    默认网络 当安装docker时,它会自动创建3个网络.可以使用docker network ls 来查看.   zane@zane-V:~$ docker network ls NETWORK ID ...

  2. ios内存管理笔记(三)

    我们在进行iOS开发时,经常会在类的声明部分看见类似于@synthesize window=_window; 的语句,那么,这个window是什么,_ window又是什么,两个东西分别怎么用,这是一 ...

  3. 在红米note4上实现自动安装软件

    因为要做自动化测试,需要对已发布的包进行回归手测,这个时候需要手动安装APK,但是红米会弹出继续安装的按钮,手点一次比较烦,想自动点"继续安装"按钮! 感谢先行者们的分享 本文参考 ...

  4. Scut游戏服务器引擎之Unity3d接入

    Scut提供Unity3d Sdk包,方便开发人员快速与Scut游戏服务器对接: 先看Unity3d示例如下: 启动Unity3d项目 打开Scutc.svn\SDK\Unity3d\Assets目录 ...

  5. 更改VS2010 工程名的方法

    哇~~~~~~~啦啦啦~~~~~~~~ 太开心了,通过不断的尝试,我终于知道怎么更改VS2010的工程名了!!! 下面分享给大家: 1.打开自己想要更改名字的工程,用ctrl+h在整个项目中把想更改的 ...

  6. web.config配置数据库连接 【转】

    http://www.cnblogs.com/breezeblew/archive/2008/05/01/1178719.html 第一种: 取连接字符串 = System.Web.Configura ...

  7. overlay和overlay2的区别

    docker作为一个容器平台,它有一套自己的存储系统.它支持的driver有overlay,overlay2, aufs等等. 这篇文章主要分析overlay和overlay2的区别. overlay ...

  8. 远程桌面连接centos 7

    首先安装tigervnc-server: yum install tigervnc-server 安装好后,设置 vi /etc/sysconfig/vncservers [root@gateway- ...

  9. Andfix热修复框架原理及源代码解析-上篇

    热补丁介绍及Andfix的使用 Andfix热修复框架原理及源代码解析-上篇 Andfix热修复框架原理及源代码解析-下篇 1.不知道怎样使用的同学,建议看看我上一篇写的介绍热补丁和Andfix的使用 ...

  10. dedecms织梦后台password忘记了怎么办?dedecms织梦后台password忘记怎样找回password?

    方法一:自己用解密的方式 用phpmyadmin登陆后台数据库,查看 找到password:去除前三位和后一位,然后拷贝到http://www.cmd5.com/在线解密工具里面解密 watermar ...