问题:

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. BBED的安装及简单的使用方法

    记录下BBED的安装--ORACLE 11.2.0.4下 1.在ORACLE 11G下安装BBED,需要从ORACLE 10G中复制三个包 bbedus.msb               sbbdp ...

  2. 在windows下如何配置RTT开发环境?

    之前一直使用MDK查看和编译RTT的源码,这几天无聊想起RTT官方提供使用scons工具编译RTT,因此想试试这种方法,做下此笔记,以供入门者参考. 注 1 下载安装Python2.7 论坛中很多人说 ...

  3. Android 计时与倒计时

    方法一 Timer与TimerTask(Java实现) [java]  view plain copy print ?   public class timerTask extends Activit ...

  4. lua中打印所以类型功能实现table嵌套table

    lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...

  5. EasyUI - Combo组件

    -自定义下拉组件,有别于ComboBox下拉组件. 效果: html代码: <input id ="box" /> <div id ="fruits&q ...

  6. sharepoint 2010 在自定义列表的字段上增加功能菜单

    sharepoint 2010 在自定义列表的字段上增加功能菜单方法 打开sharepoint designer 2010,找到需要修改的视图页面,例如allitem.aspx,编辑这个页面,点击高级 ...

  7. Java设计模式---外观模式

    外观模式(Facade) 外观模式的意图是:为子系统提供一个接口,便于它的使用.   解释: 简单的说,外观模式就是封装多个上层应用需要的方法,使得上层调用变得简单,为上层提供简单的接口,是设计模式中 ...

  8. Android高手进阶——Adapter深入理解与优化

    Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...

  9. SAP ABAP exporting list to memory ...SUBMIT 程序传输屏幕参数

    SUBMIT report EXPORTING LIST TO MEMORY              AND RETURN. submit 关键字的作用就是在程序内部调用一个程序,and retur ...

  10. Delphi颜色的表示(一共5种表示法)

    //全以红色举例: //1. RGB 模式:Self.Color := $0000ff; //不过和HTML.PhotoShop.FireWorks中的 #ff0000 是完全反的,应该叫 BGR. ...