您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關C++中怎么實現LeetCode,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
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% 正說明了這道題的難度,博主也是研究了網上別人的解法很久才看懂,然后照葫蘆畫瓢的寫了出來,下面這種解法的核心思想是 BFS,大概思路如下:目的是找出所有的路徑,這里建立一個路徑集 paths,用以保存所有路徑,然后是起始路徑p,在p中先把起始單詞放進去。然后定義兩個整型變量 level,和 minLevel,其中 level 是記錄循環中當前路徑的長度,minLevel 是記錄最短路徑的長度,這樣的好處是,如果某條路徑的長度超過了已有的最短路徑的長度,那么舍棄,這樣會提高運行速度,相當于一種剪枝。還要定義一個 HashSet 變量 words,用來記錄已經循環過的路徑中的詞,然后就是 BFS 的核心了,循環路徑集 paths 里的內容,取出隊首路徑,如果該路徑長度大于 level,說明字典中的有些詞已經存入路徑了,如果在路徑中重復出現,則肯定不是最短路徑,所以需要在字典中將這些詞刪去,然后將 words 清空,對循環對剪枝處理。然后取出當前路徑的最后一個詞,對每個字母進行替換并在字典中查找是否存在替換后的新詞,這個過程在之前那道 Word Ladder 里面也有。如果替換后的新詞在字典中存在,將其加入 words 中,并在原有路徑的基礎上加上這個新詞生成一條新路徑,如果這個新詞就是結束詞,則此新路徑為一條完整的路徑,加入結果中,并更新 minLevel,若不是結束詞,則將新路徑加入路徑集中繼續循環。寫了這么多,不知道你看暈了沒有,還是看代碼吧,這個最有效:
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; } };
看完上述內容,你們對C++中怎么實現LeetCode有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。