问题:

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(字梯)的更多相关文章

  1. [Leetcode][JAVA] Word Ladder II

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

  2. [Leetcode][JAVA] Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

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

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

  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 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  6. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  7. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  8. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

  9. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

随机推荐

  1. int 转换成 CString(VC2008里有这个问题)

    int s = 123; CString str; str.Format("%d",s); 这样就可以了,但是有的会提示这个错误 如果出现这个错误,就改成下面这个就OK了:  st ...

  2. 九度OnlineJudge之1017:还是畅通工程

    题目描述:     某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可 ...

  3. (转载)Jvm工作原理学习笔记

    一.        JVM的生命周期 1.      JVM实例相应了一个独立执行的java程序它是进程级别 a)    启动.启动一个Java程序时.一个JVM实例就产生了.不论什么一个拥有publ ...

  4. db2 用户权限

        DB2数据库权限分为实例级权限(SYSADM.SYSCTRL.SYSMAINT.SYSMON)和DB2数据库级权限(DBAMD.LOAD).DB2中用户所拥有的权限主要考虑三个方面:实例级.数 ...

  5. windows live writer插件说明文档(附录网盘地址)

    百度云地址:http://pan.baidu.com/s/1hqnjzjY 1.Screen Capture tool 用于直接在WLWriter中进行截图的一个插件,要配合SnagIt 这个软件使用 ...

  6. 页面爬虫(获取其他页面HTML)加载到自己页面

    //前台 <div id="showIframe"></div> $(document).ready(function() { var url = &quo ...

  7. .NET - 代码重构技巧

    通过面向对象三大特性:封装.继承.多态的学习,可以说我们已经掌握了面向对象的核心.接下来的学习就是如何让我们的代码更优雅.更高效.更易读.更易维护.当然了,这也是从一个普通程序员到一个高级程序员的必由 ...

  8. WPF程序长时间无人操作

    在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面. 判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标 ...

  9. OpenLayers学习笔记5——使用jQuery UI实现查询并标注(UI篇)

    近期事情非常多,老板给的压力也非常大.经常出差,另外项目和个人研究还都要跟上,本月要交论文,还要写专利,仅仅能抽时间来学习其它的东西了. 关于OpenLayers的在博客中不会写太多详细的实现(网上有 ...

  10. 四个机器学习一步一步入门约束波尔兹曼机RBM