博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ - Word Ladder 2
阅读量:7254 次
发布时间:2019-06-29

本文共 2807 字,大约阅读时间需要 9 分钟。

我发现在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
> findLadders(String start, String end, HashSet
dict) { 9 10 //for BFS 11 LinkedList
queue = new LinkedList
(); 12 //store the words that have visited and in the dict, and its corresponding level 13 HashMap
visited = new HashMap
(); 14 //the level information for every word 15 LinkedList
level = new LinkedList
(); 16 //the word and its parents 17 HashMap
> wP = new HashMap
>(); 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
p = wP.get(end); 44 p.add(par); 45 wP.put(end, p); 46 }else{ 47 ArrayList
p = new ArrayList
(); 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
p = wP.get(changed); 65 p.add(par); 66 wP.put(changed, p); 67 }else{ 68 ArrayList
p = new ArrayList
(); 69 p.add(par); 70 wP.put(changed, p); 71 } 72 } 73 } 74 } 75 76 } 77 } 78 ArrayList
> fl =new ArrayList
>(); 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
> wP, String word, ArrayList
> fl, 94 ArrayList
cur){ 95 if(wP.get(word)==null) 96 { 97 ArrayList
next = new ArrayList
(); 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
next = new ArrayList
();106 next.add(word);107 if(cur!=null && cur.size()>0)108 next.addAll(cur);109 traceback(wP, p, fl, next);110 }111 }

 

转载于:https://www.cnblogs.com/echoht/p/3700804.html

你可能感兴趣的文章
hadoop搭建与eclipse开发环境设置
查看>>
封装一个信号量集操作函数的工具
查看>>
职责要求
查看>>
java反射机制
查看>>
哈哈,好一个 uri,
查看>>
LVM扩容
查看>>
三:简单工厂模式
查看>>
正则表达式元字符
查看>>
【vue系列】elementUI 穿梭框右侧获取当前选中项的值的思路
查看>>
C语言常用函数手册
查看>>
laravel and lumen 软删除操作
查看>>
2015秋季书籍阅读计划
查看>>
数据集---Zachary's karate club---等
查看>>
Django之Form组件
查看>>
jquery validate.js 不能验证
查看>>
html的异步调用
查看>>
请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
查看>>
电子电路基础——电感、磁珠
查看>>
Django tutorial part2
查看>>
loj10098 分离的路径
查看>>