C++實現(xiàn)LeetCode(126.詞語階梯之二)
[LeetCode] 126. 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.
個人感覺這道題是相當有難度的一道題,它比之前那道 Word Ladder 要復雜很多,全場第四低的通過率 12.9% 正說明了這道題的難度,博主也是研究了網(wǎng)上別人的解法很久才看懂,然后照葫蘆畫瓢的寫了出來,下面這種解法的核心思想是 BFS,大概思路如下:目的是找出所有的路徑,這里建立一個路徑集 paths,用以保存所有路徑,然后是起始路徑p,在p中先把起始單詞放進去。然后定義兩個整型變量 level,和 minLevel,其中 level 是記錄循環(huán)中當前路徑的長度,minLevel 是記錄最短路徑的長度,這樣的好處是,如果某條路徑的長度超過了已有的最短路徑的長度,那么舍棄,這樣會提高運行速度,相當于一種剪枝。還要定義一個 HashSet 變量 words,用來記錄已經(jīng)循環(huán)過的路徑中的詞,然后就是 BFS 的核心了,循環(huán)路徑集 paths 里的內(nèi)容,取出隊首路徑,如果該路徑長度大于 level,說明字典中的有些詞已經(jīng)存入路徑了,如果在路徑中重復出現(xiàn),則肯定不是最短路徑,所以需要在字典中將這些詞刪去,然后將 words 清空,對循環(huán)對剪枝處理。然后取出當前路徑的最后一個詞,對每個字母進行替換并在字典中查找是否存在替換后的新詞,這個過程在之前那道 Word Ladder 里面也有。如果替換后的新詞在字典中存在,將其加入 words 中,并在原有路徑的基礎上加上這個新詞生成一條新路徑,如果這個新詞就是結(jié)束詞,則此新路徑為一條完整的路徑,加入結(jié)果中,并更新 minLevel,若不是結(jié)束詞,則將新路徑加入路徑集中繼續(xù)循環(huán)。寫了這么多,不知道你看暈了沒有,還是看代碼吧,這個最有效:
class Solution { public: vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { vector<vector<string>> res; unordered_set<string> dict(wordList.begin(), wordList.end()); vector<string> p{beginWord}; queue<vector<string>> paths; paths.push(p); int level = 1, minLevel = INT_MAX; unordered_set<string> words; while (!paths.empty()) { auto t = paths.front(); paths.pop(); if (t.size() > level) { for (string w : words) dict.erase(w); words.clear(); level = t.size(); if (level > minLevel) break; } string last = t.back(); for (int i = 0; i < last.size(); ++i) { string newLast = last; for (char ch = 'a'; ch <= 'z'; ++ch) { newLast[i] = ch; if (!dict.count(newLast)) continue; words.insert(newLast); vector<string> nextPath = t; nextPath.push_back(newLast); if (newLast == endWord) { res.push_back(nextPath); minLevel = level; } else paths.push(nextPath); } } } return res; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/126
類似題目:
參考資料:
https://leetcode.com/problems/word-ladder-ii/
http://yucoding.blogspot.com/2014/01/leetcode-question-word-ladder-ii.html
https://leetcode.com/problems/word-ladder-ii/discuss/40487/Java-Solution-with-Iteration
到此這篇關(guān)于C++實現(xiàn)LeetCode(126.詞語階梯之二)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)詞語階梯之二內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pipes實現(xiàn)LeetCode(193.驗證電話號碼)
這篇文章主要介紹了Pipes實現(xiàn)LeetCode(193.驗證電話號碼),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08c++中為什么可以通過指針或引用實現(xiàn)多態(tài)詳解
這篇文章主要給大家介紹了關(guān)于c++中為何可以通過指針或引用實現(xiàn)多態(tài),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04c++中explicit與mutable關(guān)鍵字的深入探究
這篇文章主要給大家介紹了關(guān)于c++中explicit與mutable關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05C++ txt 文件讀取,并寫入結(jié)構(gòu)體中的操作
這篇文章主要介紹了C++ txt 文件讀取,并寫入結(jié)構(gòu)體中的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12