題目:
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 intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Return
[
["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"]
]
解答:
public class Solution { List> result; List
list; Map > map; public List > findLadders(String beginWord, String endWord, Set
wordList) { result = new ArrayList >(); if (wordList.size() == 0) return result; list = new LinkedList
(); map = new HashMap >(); int curt = 1, next = 0; boolean found = false; Set unvisited = new HashSet (wordList); Set visited = new HashSet (); Queue queue = new ArrayDeque (); queue.add(beginWord); unvisited.add(endWord); unvisited.remove(beginWord); //BFS while (!queue.isEmpty()) { String word = queue.poll(); curt--; for (int i = 0; i < word.length(); i++) { StringBuilder sb = new StringBuilder(word); for (char ch = "a"; ch <= "z"; ch++) { sb.setCharAt(i, ch); String newWord = sb.toString(); if (unvisited.contains(newWord)) { if (visited.add(newWord)) { next++; queue.add(newWord); } if (!map.containsKey(newWord)) { map.put(newWord, new LinkedList ()); } map.get(newWord).add(word); if (newWord.equals(endWord) && !found) found = true; } } } if (curt == 0) { if (found) break; curt = next; next = 0; unvisited.removeAll(visited); visited.clear(); } } backTrace(endWord, beginWord); return result; } public void backTrace(String word, String beginWord) { if (word.equals(beginWord)) { list.add(0, beginWord); result.add(new ArrayList (list)); list.remove(0); return; } list.add(0, word); if (map.get(word) != null) { for (String s : map.get(word)) { backTrace(s, beginWord); } } list.remove(0); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65031.html
摘要:題目要求相比于,要求返回所有的最短路徑。至于如何生成該有向圖,則需要通過廣度優先算法,利用隊列來實現。將每一層的分別入棧。如果遇到則至該層結尾廣度優先算法結束。通過這種方式來防止形成圈。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find all shortest transfo...
摘要:存放過程中的所有集合為所有的結尾,則順序存放這個結尾對應的中的所有存放同一個循環的新加入的,在下一個循環再依次對其中元素進行進一步的把首個字符串放入新,再將放入,并將鍵值對放入,進行初始化 Problem Given two words (start and end), and a dictionary, find all shortest transformation sequenc...
摘要:題目解答主要解題思路的,把每一種可能的都放進去試,看能不能有一條線邊到代碼當然,這樣的時間還不是最優化的,如果我們從兩頭掃,掃到中間任何一個能夠串聯起來都可以,如果沒有找到可以串聯的那么返回。 題目:Given two words (beginWord and endWord), and a dictionarys word list, find the length of short...
摘要:另外,為了避免產生環路和重復計算,我們找到一個存在于字典的新的詞時,就要把它從字典中移去。代碼用來記錄跳數控制來確保一次循環只計算同一層的節點,有點像二叉樹遍歷循環這個詞從第一位字母到最后一位字母循環這一位被替換成個其他字母的情況 Word Ladder Given two words (beginWord and endWord), and a dictionary, find t...
摘要:但是這種要遍歷所有的情況,哪怕是已經超過最小操作次數的情況,導致代碼超時。其實從另一個角度來說,這道題可以看做是廣度優先算法的一個展示。按上文中的題目為例,可以將廣度優先算法寫成以下形式。 題目要求 Given two words (beginWord and endWord), and a dictionarys word list, find the length of short...
閱讀 785·2023-04-26 00:30
閱讀 2690·2021-11-23 09:51
閱讀 1045·2021-11-02 14:38
閱讀 2560·2021-09-07 10:23
閱讀 2243·2021-08-21 14:09
閱讀 1363·2019-08-30 10:57
閱讀 1603·2019-08-29 11:20
閱讀 1149·2019-08-26 13:53