leetcode--Word Ladder II
来源:互联网 发布:免费花生壳顶级域名 编辑:程序博客网 时间:2024/06/14 06:10
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
Return
[ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ]
Note:
- All words have the same length.
- All words contain only lowercase alphabetic characters.
题意:
给定两个单词(起始单词和终止单词),和一本字典。找到从起始单词到终止单词的最短转换路径。
要求:每次只能转换一个字母
每个转换后的单词都必须在字典里面
例如”hit“为起始单词,"cog"为终止单词
字典为{"hot","dot","dog","lot","log"}
其中一条最短路径为"hot","dot","dog","lot","log"
Note: 所有的单词长度都相同
单词中都是小写字母
分类:字符串,图,广度搜索
解法1:基本思路和Word Ladder相同。关键在于存储所有的路径。
为了存储路径,需要为每个节点增加一个pre指针指向它的前一个节点。
另外注意,在遍历某一层的时候,如果节点a找到了下一个节点next,不要马上将其从字典里面删除
因为这一层的某个节点b,也可能下一层节点也是它next。
如果你删除了,那么这个b就找不到next了,而这也是一条路径。
所以合适的做法是,当这一层全部遍历完,我们才删除已经访问过的下一层节点。
这就需要我们记录下一层访问过的节点,在代码里面我们用visited来记录。
原文链接http://blog.csdn.net/crazy__chen/article/details/47337157
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- [LeetCode] Word Ladder II
- Word Ladder II -- LeetCode
- Leetcode: Word Ladder II
- leetcode-Word Ladder II
- leetcode-Word Ladder II
- leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode: Word Ladder II
- ApplicationContextAware 获取任意bean信息
- js.Switch语句,自增自减运算
- iOS 关于手机权限的检查与获取
- C++ Primer 学习笔记-基础知识(一)
- Hotspot g1垃圾回收器
- leetcode--Word Ladder II
- 页面引用百度地图
- docker私有仓库搭建 使用Portus管理docker registry 私有仓库
- java之hashCode()和equals()详解
- JUST KIDDING 0001 指鹿为马排序
- 快速知道当前类的类名
- 144. Binary Tree Preorder Traversal
- mysqldump原理和重要参数
- dnw驱动更新,支持全系统(xp,win7,win8/win10)