未验证 提交 88ad9020 编写于 作者: K Keqi Huang 提交者: GitHub

Update 0126._Word_Ladder_II.md

上级 bc2ac777
### 126. Word Ladder II
# 126. Word Ladder II
题目:
**<font color=red>难度: Hard</font>**
<https://leetcode.com/problems/word-ladder-ii/>
## 刷题内容
难度:
> 原题连接
Hard
* https://leetcode.com/problems/word-ladder-ii/
> 内容描述
```
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
Only one letter can be changed at a time
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
Return an empty list 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.
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
```
## 解题方案
> 思路 1
******- 时间复杂度: O(len(word) * len(dictionary))******- 空间复杂度: O(len(word))******
其实关键在于怎么优化和表示图
......@@ -43,7 +86,8 @@ Hard
routine 字典,然后再根据这个来寻找路径
`{'cog': ['log', 'dog'], 'hit': [], 'log': ['lot'], 'dog': ['dot'], 'hot': ['hit'], 'lot': ['hot'], 'dot': ['hot']}`
```{'cog': ['log', 'dog'], 'hit': [], 'log': ['lot'], 'dog': ['dot'], 'hot': ['hit'], 'lot': ['hot'], 'dot': ['hot']}```
```'cog': ['log', 'dog']```这里的意思就是说在走到```'cog'```之前尝试过了```'log'```和```'dog'```,即previous tried node
......@@ -60,7 +104,6 @@ class Solution(object):
:type wordList: List[str]
:rtype: List[List[str]]
"""
def backtrack(result, trace, path, word):
if len(trace[word]) == 0:
result.append([word] + path)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册