单词接龙问题
Posted Alice_yufeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单词接龙问题相关的知识,希望对你有一定的参考价值。
class Solution
Map<String, Integer> wordId = new HashMap<String, Integer>();
List<List<Integer>> edge = new ArrayList<List<Integer>>();
int nodeNum = 0;
public int ladderLength(String beginWord, String endWord, List<String> wordList)
for (String word : wordList)
addEdge(word);
addEdge(beginWord);
if (!wordId.containsKey(endWord))
return 0;
int[] dis = new int[nodeNum];
Arrays.fill(dis, Integer.MAX_VALUE);
int beginId = wordId.get(beginWord), endId = wordId.get(endWord);
dis[beginId] = 0;
Queue<Integer> que = new LinkedList<Integer>();
que.offer(beginId);
while (!que.isEmpty())
int x = que.poll();
if (x == endId)
return dis[endId] / 2 + 1;
for (int it : edge.get(x))
if (dis[it] == Integer.MAX_VALUE)
dis[it] = dis[x] + 1;
que.offer(it);
return 0;
public void addEdge(String word)
addWord(word);
int id1 = wordId.get(word);
char[] array = word.toCharArray();
int length = array.length;
for (int i = 0; i < length; ++i)
char tmp = array[i];
array[i] = '*';
String newWord = new String(array);
addWord(newWord);
int id2 = wordId.get(newWord);
edge.get(id1).add(id2);
edge.get(id2).add(id1);
array[i] = tmp;
public void addWord(String word)
if (!wordId.containsKey(word))
wordId.put(word, nodeNum++);
edge.add(new ArrayList<Integer>());
以上是关于单词接龙问题的主要内容,如果未能解决你的问题,请参考以下文章