Java Word Ladder(字梯)
问题:
Given two words (start and end), and a dictionary, find the length of shortest transformation
sequence from start to end, 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. Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
答案:
import java.util.HashSet;
import java.util.LinkedList; public class WordLadderTest1 { /**
* @param args
*/
public static void main(String[] args) {
String start = "hit";
String end = "cog";
HashSet<String> dict = new HashSet<String>();
dict.add("hot");
dict.add("dot");
dict.add("dog");
dict.add("lot");
dict.add("cog");
System.out.println(ladderLength(start, end, dict));
} public static int ladderLength(String start, String end, HashSet<String> dict) { if (dict.size() == 0)
return 0; LinkedList<String> wordQueue = new LinkedList<String>();
LinkedList<Integer> distanceQueue = new LinkedList<Integer>(); wordQueue.add(start);
distanceQueue.add(1); while(!wordQueue.isEmpty()){
String currWord = wordQueue.pop();
Integer currDistance = distanceQueue.pop();
if(currWord.equals(end)){
return currDistance;
}
for(int i=0; i<currWord.length(); i++){
char[] currCharArr = currWord.toCharArray();
for(char c='a'; c<='z'; c++){
currCharArr[i] = c; String newWord = new String(currCharArr);
if(dict.contains(newWord)){
wordQueue.add(newWord);
distanceQueue.add(currDistance+1);
dict.remove(newWord);
}
}
}
}
return 0;
}
}
Java Word Ladder(字梯)的更多相关文章
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode][JAVA] Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 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 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- [LeetCode#128]Word Ladder II
Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
随机推荐
- 解决Charles Response 中文乱码
Response中文乱码:在Info.plist 中 的vmoption 添加-Dfile.encoding=UTF-8 info.plist路径 应用程序->Charles.app->显 ...
- 设置MAVEN_OPTS环境变量
运行mvn命令实际上是执行了Java命令,既然是运行Java,那么运行Java命令可用的参数当然也应该在运行mvn命令时可用.这个时候,MAVEN_OPTS环境变量就能派上用场. 通常需要设置MAVE ...
- backbone入门小例子
最近听了个backbone的分享,为了避免听不懂,就先做了个小例子 例子很简单,效果如下 基本视图模板: <script type="tex/template" id=&qu ...
- C#日志工具汇总
log4net log4net是一个可以帮助程序员把日志信息输出到各种不同目标的.net类库.它可以容易的加载到开发项目中,实现程序调试和运行的时候的日志信息输出,提供了比.net自 ...
- perl 循环类选择器 ,爬取内容
jrhmpt01:/root/lwp/0526# cat 0526.txt <div class="TXD_sy_title"><span class=" ...
- JavaScript 进阶(四)解密闭包closure
闭包(closure)是什么东西 我面试前端基本都会问一个问题"请描述一下闭包".相当多的应聘者的反应都是断断续续的词,“子函数”“父函数”“变量”,支支吾吾的说不清楚.我提示说如 ...
- hdu1298 T9(手机输入法,每按一个数字,找出出现频率最高的字串,字典树+DFS)
Problem Description A while ago it was quite cumbersome to create a message for the Short Message Se ...
- poj 2126 Factoring a Polynomial 数学多项式分解
题意: 给一个多项式,求它在实数域内的可分解性. 分析: 代数基本定理. 代码: //poj 2126 //sep9 #include <iostream> using namespace ...
- swift-var/let定义变量和常量
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- 炮塔郝梦主solo
尊重开发人员的工作,转载时请注明出处:http://blog.csdn.net/haomengzhu/article/details/31885287 或许你会由于它爱上dota: 或许你会由于它爱上 ...