题目:
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 transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
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.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
链接:https://leetcode.com/problems/word-ladder/#/description
6/8/2017
很开始做word ladder 2时候把思路理解了,虽然Java语法有很多小错误,但是还是通过了。思路就是建立每个word的neighbor list,其中在建的过程中可以把beginWord也算进去,算完了就取出来好了。然后bfs找到最短的路径。
239ms, 22%,运行时间有点长,不过不是大问题。
注意的问题:
1. step一进入循环就要加,否则本轮退出就计算不了了
2. visited和wordNeighbor里面有beginWord是没有关系的。这样第19行可以更简洁。做法是第33和50行
3. buildNeighborMap那部分里面对每一个word的每一个位置更换26个字母
4. 每到一个新word先wordNeighbor.put(word, new ArrayList<String>())之后再wordNeighbor.get(word).add(tempString)不可以用,不知道为什么。
1 public class Solution { 2 public int ladderLength(String beginWord, String endWord, List<String> wordList) { 3 // build map Map<String, List<String>> 4 Map<String, List<String>> wordNeighbor = buildNeighborMap(wordList, beginWord); 5 // bfs 6 Set<String> visited = new HashSet<String>(); // could include beginWord 7 Queue<String> queue = new LinkedList<String>(); 8 int step = 0; 9 queue.offer(beginWord); 10 while (!queue.isEmpty()) { 11 int size = queue.size(); 12 step++; 13 for (int i = 0; i < size; i++) { 14 String transform = queue.poll(); 15 if (visited.contains(transform)) { 16 continue; 17 } 18 visited.add(transform); 19 if (transform.equals(endWord)) { 20 return step; 21 } 22 for (String n: wordNeighbor.get(transform)) { 23 queue.offer(n); 24 } 25 } 26 } 27 return 0; 28 } 29 private Map<String, List<String>> buildNeighborMap(List<String> wordList, String beginWord) { 30 Map<String, List<String>> wordNeighbor = new HashMap<String, List<String>>(); 31 Set<String> wordSet = new HashSet<String>(wordList); 32 // add beginWord, easy to find its neighbor 33 wordList.add(beginWord); 34 for (String word: wordList) { 35 int wordLength = word.length(); 36 List<String> neighbors = new ArrayList<String>(); 37 for (int i = 0; i < wordLength; i++) { 38 for (char c = 'a'; c <= 'z'; c++) { 39 StringBuilder temp = new StringBuilder(word); 40 temp.setCharAt(i, c); 41 String tempString = temp.toString(); 42 if (wordSet.contains(tempString)) { 43 neighbors.add(tempString); 44 } 45 } 46 } 47 wordNeighbor.put(word, neighbors); 48 } 49 // remove beginWord, since it's not a transformed word 50 wordList.remove(wordList.size() - 1); 51 return wordNeighbor; 52 } 53 }
别人关于时间复杂度的理解
但是我认为时间复杂度没有那么高???
http://www.cnblogs.com/yrbbest/p/4438488.html
更多讨论