leetCode127 . Word Ladder Java版 (单词等长变换)

转载地址:https://blog.csdn.net/mine_song/article/details/68951974

127. Word Ladder

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

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note thatbeginWord isnot 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.

UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

无向图求最短路 - BFS 中的level order

给出两个单词(start和end)和一个字典,找到从start到end的最短转换序列

比如:

  1. 每次只能改变一个字母。
  2. 变换过程中的中间单词必须在字典中出现。
思路:这时一道bfs的题,开始突然反应不过来。 可以想象成一棵树,根节点是start字符串,第二层是所有的和它相差一个字母的字符串(之前出现过的,之后就没有必要出现了,因为出现的话,也是abc变成bbc又变回abs,没有意义),用一个hashmap来保存每一个节点的所处的层数,还需要一个队列来实现广度优先搜索,因为是从顶层到底层来遍历的,所以发现等于end的时候的层数值就是最小的,返回即可。

[java]  view plain  copy
  1. import java.util.*;  
  2. public class word {  
  3.     public int ladderLength(String beginWord, String endWord, List<String> wordList) {  
  4.         if (beginWord == null || endWord == null || beginWord.length() == 0 || endWord.length() == 0  
  5.                 || beginWord.length() != endWord.length())  
  6.             return 0;  
  7.         // 此题关键是去重,还有去除和beginWord,相同的单词  
  8.         Set<String> set = new HashSet<String>(wordList);  
  9.         if (set.contains(beginWord))  
  10.             set.remove(beginWord);  
  11.         Queue<String> wordQueue = new LinkedList<String>();  
  12.         int level = 1// the start string already count for 1  
  13.         int curnum = 1;// the candidate num on current level  
  14.         int nextnum = 0;// counter for next level  
  15.         // 或者使用map记录层数  
  16.         // Map<String, Integer> map = new HashMap<String, Integer>();  
  17.         // map.put(beginWord, 1);  
  18.         wordQueue.add(beginWord);  
  19.   
  20.         while (!wordQueue.isEmpty()) {  
  21.             String word = wordQueue.poll();  
  22.             curnum--;  
  23.             // int curLevel = map.get(word);  
  24.             for (int i = 0; i < word.length(); i++) {  
  25.                 char[] wordunit = word.toCharArray();  
  26.                 for (char j = 'a'; j <= 'z'; j++) {  
  27.                     wordunit[i] = j;  
  28.                     String temp = new String(wordunit);  
  29.   
  30.                     if (set.contains(temp)) {  
  31.                         if (temp.equals(endWord))  
  32.                             // return curLevel + 1;  
  33.                             return level + 1;  
  34.                         // map.put(temp, curLevel + 1);  
  35.                         nextnum++;  
  36.                         wordQueue.add(temp);  
  37.                         set.remove(temp);  
  38.                     }  
  39.                 }  
  40.             }  
  41.             if (curnum == 0) {  
  42.                 curnum = nextnum;  
  43.                 nextnum = 0;  
  44.                 level++;  
  45.             }  
  46.         }  
  47.         return 0;  
  48.     }  
  49. }  

猜你喜欢

转载自blog.csdn.net/u013560925/article/details/80202613