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 ...
随机推荐
- jquery mobile自己定义webapp开发实例(一)——前言篇
用jquery mobile做了一段时间的webapp开发,准备用自己的一个小demo做一个模块化的分享 点击demo演示 手机演示二维码: 此demo已经是比較老的版本号,用户体验流畅度确实还存在非 ...
- Swift - 使用CGBlendMode改变UIImage颜色
类似于PS,Swift中也可对图片UIImage进行图层混合(blending),而且提供了相当丰富的混合模式(blendMode).本文先介绍使用其中的kCGBlendModeDestination ...
- [Cocos2d-x开发问题-3] cocos2dx动画Animation介绍
Cocos2d-x为了减少开发难度,对于动画的实现採用的帧动画的方案.这也就是说Cocos2d-x中的动画是帧动画. 帧动画的原理相信大家都不陌生,就是多张图片循环播放以实现动画的效果. 一个简单的动 ...
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
HTML5 Canvas自定义圆角矩形与虚线(RoundedRectangle and Dash Line) 实现向HTML Canvas 2d context绘制对象中添加自定义的函数功能演示,如何 ...
- KaOS 2016.04 发布,桌面 Linux 发行版
KaOS 2016.04 发布了,KaOS是一份桌面Linux发行,其特色在于最新版本的KDE桌面环境及其他流行的使用Qt工具包的软件程序.它最初基于Arch Linux,但从2013年四月起,开发者 ...
- 菜单组件——axure线框图部件库介绍
软件类的教程,我写不出长篇大论,这里面的都是基础的操作,希望初学者,根据一个功能演示,可以自己测试其他功能菜单的效果! Axure自带的菜单组件,我几乎没有用到过,做菜单导航,我第一时间想到的还是矩形 ...
- haproxy timeout server 46000 后台超时时间
[root@wx03 ~]# sh ./1.sh Wed Jul 6 19:54:40 CST 2016 <html><body><h1>504 Gateway T ...
- abap优化工具事务代码: ST05
- linux:sed高级命令之n、N(转)
sed的语法格式: sed [option] {sed-command} {input-file} sed在正常情况下,将处理的行读入模式空间(pattern space),脚本中的“sed-comm ...
- poj2418 map 快排水过
/*计算每个单词的百分比按字典序输出*/ #include <cstdio>#include<cstdlib>#include <cstring>#include ...