Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time
- 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的更多相关文章
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [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用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- 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
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- [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 _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- iphone开发-SQLite数据库使用
我现在要使用SQLite3.0创建一个数据库,然后在数据库中创建一个表格. 首先要引入SQLite3.0的lib库.然后包含头文件#import <sqlite3.h> [1]打开数据库, ...
- AIX 删除指定目录、指定后缀、指定天数以前的历史文件
find /home/oracle/admin/zhjport/udump/ -name "*.trc" -mtime +30 -exec rm {} \; 如果要自动执行可以加入 ...
- 【IntelliJ Idea】idea快速创建maven spring项目
想试试AOP在spring的web项目上的使用情况,所以想尽快使用idea快速的搭建一个spring的web项目,当然,是maven管理的项目 步骤如下: 1.打开idea 左上角file---> ...
- 【ActiveMQ】1.下载安装启动使用
官网下载:http://activemq.apache.org/activemq-5121-release.html 官网指导文档:http://activemq.apache.org/version ...
- cocos2dx 2.x新建项目
举例: cocos2d-x-2.2.6/tools/project-creator 进入 这个文件夹 chmod 777 project-cereator.py ./create_project.py ...
- 使用 VS2010 开发 MapXtreme2008 遇到的问题 无法复制文件“C:\Program Files\Common Files\MapInfo\MapXtreme\7.0.0\Compiler.DLL”,原因是找不到该文件
将需要引用的mapxtreme的dll引用到项目中,然后右键“属性”,将"复制本地“值改成false,编译成功
- 第一个Swift程序Hello World
import Foundation print("Hello, World!") print("I am here!") var arr=["项羽&q ...
- java调试工具jdb
Finds and fixes bugs in Java platform programs. Synopsis jdb [options] [classname] [arguments] optio ...
- Python数据结构:列表、元组和字典
在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元 ...
- Apc缓存Opcode(转)
1.PHP执行 PHP的运行阶段也分成三个阶段: Parse.语法分析阶段. Compile.编译产出opcode中间码. Execute.运行,动态运行进行输出. ...