Middle-题目119:127. Word Ladder

原创 2016年05月31日 20:10:25

题目原文:
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.
题目大意:
给出两个单词beginWord和endWord,和一个字典,求出从beginWord到endWord的最短转换序列,要求中间单词必须是字典中的,且每次只能转换一个字母。
题目分析:
表面上看是一个单源最短路径问题,博客中说这道题的test case和题目要求都有问题,所以我也就没做,以下只贴出ac代码。
源码:(language:java)

public class Solution {

    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        Set<String> beginSet = new HashSet<String>(), endSet = new HashSet<String>();

        int len = 1;
        int strLen = beginWord.length();
        HashSet<String> visited = new HashSet<String>();

        beginSet.add(beginWord);
        endSet.add(endWord);
        while (!beginSet.isEmpty() && !endSet.isEmpty()) {
            if (beginSet.size() > endSet.size()) {
                Set<String> set = beginSet;
                beginSet = endSet;
                endSet = set;
            }

            Set<String> temp = new HashSet<String>();
            for (String word : beginSet) {
                char[] chs = word.toCharArray();

                for (int i = 0; i < chs.length; i++) {
                    for (char c = 'a'; c <= 'z'; c++) {
                        char old = chs[i];
                        chs[i] = c;
                        String target = String.valueOf(chs);

                        if (endSet.contains(target)) {
                            return len + 1;
                        }

                        if (!visited.contains(target) && wordList.contains(target)) {
                            temp.add(target);
                            visited.add(target);
                        }
                        chs[i] = old;
                    }
                }
            }

            beginSet = temp;
            len++;
        }

        return 0;
    }
}

成绩:
47ms,beats 89.34%,众数97ms,2.42%
Cmershen的碎碎念:
据说后面的word ladder 2这道题也是有问题的。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

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]

C++语言,使用BFS解决word ladder问题

leetcode解题笔记:127. Word Ladder

题目要求: Given two words (beginWord and endWord), and a dictionary’s word list, find the length of sho...

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

[Leetcode 126 & 127, hard & medium] Word Ladder II & I

LeetCode(127) Word Ladder

题目如下: Given two words (start and end), and a dictionary, find the length of shortest transformation ...

leetcode 127 Word Ladder

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

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 (广度优先搜索)

最近重新捡起算法来看,发现广度优先搜索的代码不是很熟,就在leetcode上搜了一下,找到这道题练练,还是发现了很多问题的。题目描述: Given two words (beginWord and ...

leetcode 127. Word Ladder

class Solution { public: int ladderLength(string beginWord, string endWord, vector& wordList) { u...
  • Kexiii
  • Kexiii
  • 2017年02月18日 20:34
  • 173

[LeetCode]127.Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequen...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目119:127. Word Ladder
举报原因:
原因补充:

(最多只允许输入30个字)