Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

由于现做的126题,所以这道题其实只用BFS就可以了。

用126的答案。

public class Solution {

    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
if( beginWord == null || beginWord.length() == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )
return 0;
return BFS(beginWord,endWord,wordList);
} public int BFS(String beginWord,String endWord,Set<String> wordList){ Queue queue = new LinkedList<String>();
queue.add(beginWord);
int result = 1;
while( !queue.isEmpty() ){
String str = (String) queue.poll();
if( str.equals(endWord) )
continue;
for( int i = 0 ;i <beginWord.length();i++){
char[] word = str.toCharArray();
for( char ch = 'a';ch<='z';ch++) {
word[i] = ch;
String Nword = new String(word);
if ( wordList.contains(Nword)) {
if (!map.containsKey(Nword)) {
map.put(Nword, (int) map.get(str) + 1);
queue.add(Nword);
}
}
if( Nword.equals(endWord) )
return (int) map.get(str) + 1;
}
}
}
return 0;
}
}

去掉map,会快一些。

public class Solution {

    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
if( beginWord == null || beginWord.length() == 0 || wordList.size() == 0 || beginWord.length() != endWord.length() )
return 0; Queue queue = new LinkedList<String>();
queue.add(beginWord);
int result = 1;
while( ! queue.isEmpty() ){
int len = queue.size();
for( int i = 0;i<len;i++){
String str = (String) queue.poll();
for( int ii = 0; ii < str.length();ii++){
char[] word = str.toCharArray();
for( char ch = 'a'; ch<='z';ch++){
word[ii] = ch;
String newWord = new String(word);
if( wordList.contains(newWord) ){
wordList.remove(newWord);
queue.add(newWord);
}
if( newWord.equals(endWord) )
return result+1;
}
}
}
result++;
}
return 0;
}
}

还有更快的做法,一般是前后一起建立队列来做,会快很多。

leetcode 127. Word Ladder ----- java的更多相关文章

  1. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  4. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  5. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  6. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  7. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. [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 ...

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

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

随机推荐

  1. C++-dynamic_cast的用处

    主要用来在没有实现文件,只有头文件的情况下,添加派生类的功能,如下例给programmer加奖金. 注意:dynamic_cast不能用于没有virtual函数的类 ///////////////// ...

  2. [转]diskpart命令

    from: http://support.microsoft.com/kb/300415/zh-cn Diskpart 与许多命令行实用工具不同,原因是它不以单行模式操作.相反,当您启动该实用工具后, ...

  3. [转]AndroidManifest.xml文件详解

    转自:http://www.cnblogs.com/greatverve/archive/2012/05/08/AndroidManifest-xml.html AndroidManifest.xml ...

  4. 开源留言板 --wekan部署

    1. 安装ubuntu--server-64位系统 2. 登录ubuntu系统 3. 下载自动安装脚本 #git clone https://github.com/anselal/wekan 4. 执 ...

  5. Xrun 将 app 转化为 IPA

    xcodebuild命令行打包,在使用xcodebuild编译后发现有些东西有些临时性质的东西,依然存在,搜索了一些资料,找到有clean的命令:在之前打包都是生成app文件,将app打包成ipa文件 ...

  6. pod创建的工程找不到库

    ld: library not found for -lAFNetworking app工程 和 Pod工程里面的所有库   Build Active Architecuture Only 所有库都设 ...

  7. java删除指定目录及其文件

    import java.io.File; public class Test { public static void main(String args[]){ Test t = new Test() ...

  8. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  9. php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动

    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动 每个人机器不一样,我手头是个air book,查了一下现在最好在mac下,用mamp, mamp百科介绍 , ...

  10. Android.mk 常用宏和变量

    android ndk开发有一个重要的文件 Android.mk,他虽然重要,但是对它进行深入介绍的文档却比较的少,这里将对Android.mk中常用的宏和变量进行说明: 由于这一部分的内容多,资料零 ...