Word Loadder II (***) -- BFS DFS
来源:互联网 发布:诚邀辣妹 网络性与爱 编辑:程序博客网 时间:2024/04/28 16:44
题目 : 和 word Loadder 不同的是他要列出所有最短的路径
思路 :
1. BFS生成从start 到 end 节点与高度的map
2. DFS从 end节点,递归遍历到start, 找出所有可能的路径
代码来自 : Leetcode Word Ladder II 解题报告
public class Solution { //记录每个单词所在的层数 HashMap<String,Integer> path = new HashMap<String,Integer>(); //bfs生成path void bfs(String start, String end, HashSet<String> dict) { Queue queue = new LinkedList<String>(); queue.add(start); path.put(start,0); String current; while(!queue.isEmpty()) { current = (String)queue.poll(); if(current==end) { continue; } for(int i=0;i<current.length();i++) { char[] strCharArr = current.toCharArray(); for(char ch='a';ch<='z';ch++) { if(strCharArr[i]==ch) { continue; } strCharArr[i] = ch; String newWord = new String(strCharArr); if(newWord.equals(end)==true||dict.contains(newWord)) { //每个单词在path中只能出现一次,也就是每个单词只能出现在一层中,这样就很巧妙的解决了环的问题。 if(path.get(newWord)==null) { int depth = (int)path.get(current); path.put(newWord,depth + 1); queue.add(newWord); } } } } } } //从目标单词往回找开始单词,记录所有路径 void dfs(String start, String end, HashSet<String> dict, ArrayList<String> pathArray,ArrayList<ArrayList<String>> result) { //找到了,需要reverse加入的所有单词 if(start.equals(end)==true) { pathArray.add(start); Collections.reverse(pathArray); result.add(pathArray); return; } if(path.get(start)==null) { return; } pathArray.add(start); int nextDepth = (int)path.get(start) - 1; for(int i=0;i<start.length();i++) { char[] strCharArr = start.toCharArray(); for(char ch='a';ch<='z';ch++) { if(strCharArr[i]==ch) { continue; } strCharArr[i] = ch; String newWord = new String(strCharArr); //只相差一个字母同时这个单词所在的层数也是当前单词的上一层 if(path.get(newWord)!=null&&(path.get(newWord)==nextDepth)) { //这里是一个新的ArrayList对象,这里就像一个分支点 ArrayList<String> newPathArray = new ArrayList<String>(pathArray); dfs(newWord,end,dict,newPathArray,result); } } } } public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) { ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>(); ArrayList<String> path = new ArrayList<String>(); if(start==null||end==null||start.length()!=end.length()) { return result; } bfs(start, end, dict); dfs(end,start, dict, path, result); return result; } }
0 0
- Word Loadder II (***) -- BFS DFS
- word Loadder I(****) -- BFS
- Word Ladder II(BFS + DFS)
- [leetcode] 126 Word Ladder ii bfs dfs
- 126. Word Ladder II(bfs+dfs)
- 126. Word Ladder II 、 127. Word Ladder(leetcode BFS+DFS)
- leetcode 126. Word Ladder II BFS + 反向链表 + DFS
- BFS Word Ladder II
- Air strike II BFS+DFS
- 【图\树的BFS\DFS】 Word Ladder II (MinPath)、Surrounded Regions 、Add Next pointer in tree、zigzag bfs Tree
- Leetcode dfs Word Break II
- leetcode Word Ladder II hash BFS
- LeetCode Word Ladder II(BFS、SPFA)
- Word Ladder II 相邻字符串接龙 BFS
- LeetCode 140. Word Break II (DP+DFS)
- Word Break II[动态规划&DFS]
- LeetCode Word Break II DP+DFS
- leetcode -- Word Search I & II -- dfs 重点
- 归并排序(含完整源码)
- 华为OJ(笔画)
- 关于单链表中环的问题
- PS学习笔记----图层锁定
- 在 WinCe 平台读写 ini 文件
- Word Loadder II (***) -- BFS DFS
- QT---设置textedit文本框中某个字符格式
- QT---qtextedit中的光标问题
- python 之 推导式
- 慕课学习C++笔记02-封装(上)
- 3-06. 表达式转换(25)
- 几道python笔试题
- leetcode 258 Add Digits(难易度:Easy)
- Java IO流->处理流->打印流:PrintStream&PrintWriter