Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, , All words have the same length. All words contain only lowercase alphabetic characters. 分析:这种题,肯定是每次改变单词的一个字母,然后逐渐搜索,很多人一开始就想到用dfs,其实像这种求最短路径、树最小深度问题bfs最适合,可以参考我的这篇博客 bfs(层序遍历)求二叉树的最小深度 。本题bfs要注意的问题: 和当前单词相邻的单词是:对当前单词改变一个字母且在字典中存在的单词 bfs队列中用NULL来标识层与层的间隔,每次碰到层的结尾,遍历深度+1 Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, All words contain only lowercase alphabetic characters.
Read full article from LeetCode:Word Ladder I II - tenos - 博客园
No comments:
Post a Comment