leetcode解题之127. Word Ladder Java版 (单词等长变换)
2017-04-02 14:21
543 查看
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:Only one letter can be changed at a time.
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的最短转换序列
比如:
每次只能改变一个字母。
变换过程中的中间单词必须在字典中出现。
思路:这时一道bfs的题,开始突然反应不过来。 可以想象成一棵树,根节点是start字符串,第二层是所有的和它相差一个字母的字符串(之前出现过的,之后就没有必要出现了,因为出现的话,也是abc变成bbc又变回abs,没有意义),用一个hashmap来保存每一个节点的所处的层数,还需要一个队列来实现广度优先搜索,因为是从顶层到底层来遍历的,所以发现等于end的时候的层数值就是最小的,返回即可。
import java.util.*;
public class word {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if (beginWord == null || endWord == null || beginWord.length() == 0 || endWord.length() == 0
|| beginWord.length() != endWord.length())
return 0;
// 此题关键是去重,还有去除和beginWord,相同的单词
Set<String> set = new HashSet<String>(wordList);
if (set.contains(beginWord))
set.remove(beginWord);
Queue<String> wordQueue = new LinkedList<String>();
int level = 1; // the start string already count for 1
int curnum = 1;// the candidate num on current level
int nextnum = 0;// counter for next level
// 或者使用map记录层数
// Map<String, Integer> map = new HashMap<String, Integer>();
// map.put(beginWord, 1);
wordQueue.add(beginWord);
while (!wordQueue.isEmpty()) {
String word = wordQueue.poll();
curnum--;
// int curLevel = map.get(word);
for (int i = 0; i < word.length(); i++) {
char[] wordunit = word.toCharArray();
for (char j = 'a'; j <= 'z'; j++) {
wordunit[i] = j;
String temp = new String(wordunit);
if (set.contains(temp)) {
if (temp.equals(endWord))
// return curLevel + 1;
return level + 1;
// map.put(temp, curLevel + 1);
nextnum++;
wordQueue.add(temp);
set.remove(temp);
}
}
}
if (curnum == 0) {
curnum = nextnum;
nextnum = 0;
level++;
}
}
return 0;
}
}
相关文章推荐
- Leetcode 127. Word Ladder 字符变换 解题报告
- leetCode 6. ZigZag Conversion(Z形变换) 解题思路及方法
- [LeetCode 127] - 单词梯(Word Ladder)
- [LeetCode] 127. Word Ladder 单词阶梯
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
- LeetCode 127. Word Ladder(单词梯子)
- Leetcode 79 单词搜索 解题报告
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
- Leetcode 126. Word Ladder II 字符变换2 解题报告
- leetcode解题笔记:127. Word Ladder
- 【LeetCode】72. Edit Distance单词变换的最少操作
- LeetCode Word Ladder 找单词变换梯
- [leetcode] 127. Word Ladder 解题报告
- (Java) LeetCode 127. Word Ladder —— 单词接龙
- 程序员面试金典——解题总结: 9.18高难度题 18.10给定两个字典里的单词,长度相等。编写一个方法,将一个单词变换成另一个单词,一次只改动一个字母。
- [Leetcode] 127. Word Ladder 解题报告
- LeetCode 102. Binary Tree Level Order Traversal 解题报告
- [leetcode] 393. UTF-8 Validation 解题报告
- HDU2072 单词数 解题报告--set
- LeetCode 137. Single Number II 解题报告