LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法。

这道题是在上一道的基础上进一步把所有可能得转换序列给出。

同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径。

下面是AC代码。


  1 /**
2 * Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end,
3 * @param start
4 * @param end
5 * @param dict
6 * @return
7 */
8 public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
9
10 //for BFS
11 LinkedList<String> queue = new LinkedList<String>();
12 //store the words that have visited and in the dict, and its corresponding level
13 HashMap<String,Integer> visited = new HashMap<String,Integer>();
14 //the level information for every word
15 LinkedList<Integer> level = new LinkedList<Integer>();
16 //the word and its parents
17 HashMap<String,ArrayList<String>> wP = new HashMap<String,ArrayList<String>>();
18 queue.offer(start);
19 level.offer(1);
20 wP.put(queue.peek(), null);//start has no parents;
21 visited.put(start, 1);
22 while(!queue.isEmpty() && (!visited.containsKey(end) || level.peek() == visited.get(end)-1)){
23 String par = queue.poll();
24 int le = level.poll();
25
26 //for every character in the word
27 for(int i=0;i<par.length();i++){
28
29 char[] words = par.toCharArray();
30 char o = words[i];//the original word
31 for(char c=‘a‘;c<=‘z‘;c++)
32 {
33 if(c!=o){
34 //subsitude by another char
35 words[i] = c;
36 String changed = new String(words);
37 //the last-1 level .
38 if(changed.equals(end))
39 {
40 visited.put(changed, le+1);// it.s very important!!!! Dont‘t forget!!
41
42 if(wP.containsKey(end)){
43 ArrayList<String> p = wP.get(end);
44 p.add(par);
45 wP.put(end, p);
46 }else{
47 ArrayList<String> p = new ArrayList<String>();
48 p.add(par);
49 wP.put(end, p);
50 }
51 }
52 //return le+1;
53 else if((visited.get(changed)==null || visited.get(changed) == le+1) && dict.contains(changed)){
54 //the condition is very important!!! otherwise, there will be duplicate.
55 if(!visited.containsKey(changed))
56 {
57 queue.offer(changed);
58 level.offer(le+1);
59 }
60 visited.put(changed,le+1);
61
62 //update the word and his parents information
63 if(wP.containsKey(changed)){
64 ArrayList<String> p = wP.get(changed);
65 p.add(par);
66 wP.put(changed, p);
67 }else{
68 ArrayList<String> p = new ArrayList<String>();
69 p.add(par);
70 wP.put(changed, p);
71 }
72 }
73 }
74 }
75
76 }
77 }
78 ArrayList<ArrayList<String>> fl =new ArrayList<ArrayList<String>>();
79 //it‘s very important!!! to Check whether it has such path
80 if(!wP.containsKey(end))
81 return fl;
82 traceback(wP,end,fl, null);
83
84 return fl;
85 }
86 /**
87 * DFS ,对每个节点的父节点进行深度遍历
88 * @param wP
89 * @param word
90 * @param fl
91 * @param cur
92 */
93 private void traceback(HashMap<String,ArrayList<String>> wP, String word, ArrayList<ArrayList<String>> fl,
94 ArrayList<String> cur){
95 if(wP.get(word)==null)
96 {
97 ArrayList<String> next = new ArrayList<String>();
98 next.add(word);
99 if(cur!=null && cur.size()>0)
100 next.addAll(cur);
101 fl.add(next);
102 return;
103 }
104 for(String p: wP.get(word)){
105 ArrayList<String> next = new ArrayList<String>();
106 next.add(word);
107 if(cur!=null && cur.size()>0)
108 next.addAll(cur);
109 traceback(wP, p, fl, next);
110 }
111 }

LeetCode OJ - Word Ladder 2,布布扣,bubuko.com

时间: 2024-08-01 06:22:24

LeetCode OJ - Word Ladder 2的相关文章

LeetCode OJ - Word Ladder

我觉得这道题比较难,主要是因为对于我来说: 1. 我没有把这个问题联想到树的宽度遍历(即便没有考虑树的宽度遍历,也是可以做的,但是我一开始实现的代码却是深度遍历,后来发现树的BFS一般使用queue实现的,貌似没有递归的方法??) 2. 即使在意识到用BFS,却还有一个陷阱:我是对字典进行了BFS,这个说实话,代码长,还TLE: 后来参考了前辈的代码,采用对26个单词进行枚举,才解决了这个问题. 下面是AC代码: 1 /** 2 * Given two words (start and end)

[LeetCode OJ] Word Search 深度优先搜索DFS

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

[LeetCode] 126. Word Ladder II_Hard tag: BFS&amp;DFS

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

【LeetCode】Word Ladder 字符串

题目:Word Ladder <span style="font-size:18px;">/**LeetCode word ladder * 题目:给定一个起始单词和一个终结单词以及一个字典,要求每次变换一个字符,成为字典中新的词,直到变为最后的词,要求其最短路径 * 思路:利用队列,先弹出第一个词,分别将词中每一个字符替换直到找到一个字典中存在的词,加入队列,直到匹配的词是最后一个,此时终止 * 如果没有这样的路径,则返回0 */ package javaTrain; i

[LeetCode][JavaScript]Word Ladder

https://leetcode.com/problems/word-ladder/ Word Ladder Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time Each

[Leetcode][JAVA] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start = "hit

Leetcode 127. Word Ladder

思路: 1 class Solution(object): 2 def __init__(self): 3 return 4 5 def ladderLength(self, beginWord, endWord, wordList): 6 """ 7 :type beginWord: str 8 :type endWord: str 9 :type wordList: List[str] 10 :rtype: int 11 """ 12 fro

[Leetcode][JAVA] Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:start

leetcode 127. Word Ladder ----- java

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time Each intermediate word must exist in the word li