leetcode 127. Word Ladder ----- java
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:
- Only one letter can be changed at a time
- 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的更多相关文章
- [LeetCode] 127. Word Ladder 单词阶梯
		Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ... 
- leetcode 127. Word Ladder、126. Word Ladder II
		127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ... 
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
		题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ... 
- Leetcode#127 Word Ladder
		原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ... 
- Java for LeetCode 127 Word Ladder
		Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ... 
- leetcode@ [127] Word Ladder (BFS / Graph)
		https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ... 
- [leetcode]127. Word Ladder单词接龙
		Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ... 
- [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 ... 
- Java for LeetCode 126 Word Ladder II 【HARD】
		Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ... 
随机推荐
- [转载]Android 编译环境 build/envsetup.sh分析
			2013-12-23 11:28:40 转载自: http://blog.csdn.net/evilcode/article/details/7005757 请到转载地址阅读原文, 转载以备查询. 
- bzoj 3529 数表 莫比乌斯反演+树状数组
			题目大意: 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. ... 
- FZU 2082 过路费
			树链剖分模板题 #include <cstdio> #include <iostream> #include <cstring> #include <algo ... 
- Android安全之WebViewUXSS漏洞
			Android安全 WebView UXSS app开发 漏洞分析 移动安全 0X01 前言 XSS是我们比较熟悉的一种攻击方式,包括存储型XSS.反射型XSS.DOM XSS等,但UXSS(通用型X ... 
- vc设置按钮文字颜色
			设置按钮文字颜色使用 CMFCBUTTON即可 在OnInitDialog函数加入如下内容即可 ((CMFCButton*)GetDlgItem(IDC_MFCBUTTON1))->SetTex ... 
- 首席技术官 (CTO) 比普通程序员强在哪
			互联网的蓬勃发展,让无数的程序员身价水涨船高,都变成了「香饽饽」,更有了不少「创业」,「当上 CTO,迎娶白富美的传说」.都说不想当元帅的士兵不是好士兵,我觉得这件事见仁见智,但提升自己的价值,让自己 ... 
- (转)html5开发之viewport使用
			原文:http://www.php100.com/html/webkaifa/HTML5/2012/0831/10979.html 随着高端手机(Andriod,Iphone,Ipod,WinPhon ... 
- 团队开发——冲刺1.a
			冲刺阶段一(第一天) 1.今天准备做什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能. 2.明天做什么:简单设计界面. 
- PHP使用mysqli操作MySQL数据库
			PHP的 mysqli 扩展提供了其先行版本的所有功能,此外,由于 MySQL 已经是一个 具有完整特性的数据库服务器 , 这为PHP 又添加了一些新特性 . 而 mysqli 恰恰也支持了 这些新特 ... 
- Date and Time
			The PHP date() function is used to format date and/or a time and formats as timestamp to a more read ... 
