题目:
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); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 768·2023-04-26 00:30
阅读 2661·2021-11-23 09:51
阅读 1026·2021-11-02 14:38
阅读 2528·2021-09-07 10:23
阅读 2207·2021-08-21 14:09
阅读 1266·2019-08-30 10:57
阅读 1590·2019-08-29 11:20
阅读 1112·2019-08-26 13:53