这是一道太难的题了,做了一天,最后进行一下记录和反思。
这道题主要是用到图的方法。包括邻接表构建图,然后用bfs,dfs去做。
主要有几点:
1、在做bfs时,尤其在建邻接图时,用HashMap<T,ArrayList<T>>,并用HashMap<T,Integer>来记录边长,并且可以用来判断图中是否已经遍历过该节点了。bfs直接用队列即可,比较简单。
2、dfs时最好用递归做,并且记得最后移除当前元素(即最后一行res1.remove(start);),这相当于是返回上一层。其实就是dfs递归套路,一定记住!!!
直接上代码:
package leetcode;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class wordLadderIIMyOwn {
public static ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
HashMap<String, ArrayList<String>> neighbours=new HashMap<String, ArrayList<String>>();
HashMap<String, Integer> distance=new HashMap<String, Integer>();
bfs(start,end,neighbours,distance,dict);
ArrayList<String> res1=new ArrayList<String>();
ArrayList<ArrayList<String>> res=new ArrayList<ArrayList<String>>();
dfs(start, end, neighbours, distance,res, res1);
return res;
}
//在做bfs时,尤其在建邻接图时,用HashMap<T,ArrayList<T>>,并用HashMap<T,Integer>来记录边长,并且可以用来判断图中是否已经遍历过该节点了
public static void bfs(String start,String end,HashMap<String, ArrayList<String>> neighbours, HashMap<String, Integer> distance,HashSet<String> dict) {
distance.put(start,0);
LinkedList<String> que=new LinkedList<String>();
que.add(start);
while(!que.isEmpty()) {
int count=que.size();
boolean found=false;
for(int i=0;i<count;i++) {
String node=que.poll();
int curDistance=distance.get(node);
ArrayList<String> ngh=findNeighbours(node,end, dict);
neighbours.put(node,new ArrayList<String>(ngh));
for(String s:ngh) {
if(!distance.containsKey(s)) {
distance.put(s,curDistance+1);
if(s.equals(end))
found=true;
else {
que.add(s);
}
}
}
if(found)
break;
}
}
return;
}
public static ArrayList<String> findNeighbours(String node,String end,HashSet<String> dict) {
ArrayList<String> res=new ArrayList<String>();
char[] c=node.toCharArray();
for(int i=0;i<c.length;i++)
n:for(char j='a';j<'z';j++) {
if(c[i]==j)
continue n;
char old=c[i];
c[i]=j;
if(String.valueOf(c)==end)
return res;
if(dict.contains(String.valueOf(c)))
res.add(String.valueOf(c));
c[i]=old;
}
return res;
}
//dfs时最好用递归做,并且记得最后移除当前元素(即最后一行res1.remove(start);),这相当于是返回上一层
public static void dfs(String start,String end,HashMap<String, ArrayList<String>> neighbours,HashMap<String,Integer> distance,ArrayList<ArrayList<String>> res,ArrayList<String> res1){
res1.add(start);
//一定记得String要用equals不能直接去==
if(start.equals(end))
res.add(new ArrayList<String>(res1));
else {
ArrayList<String> ngh=neighbours.get(start);
for(String s:ngh) {
//这块就是防止环产生
if(distance.get(s)==distance.get(start)+1)
dfs(s,end,neighbours,distance,res,res1);
}
}
res1.remove(start);
}
public static void main(String[] args) {
String start="hit";
String end="cog";
HashSet<String> dict=new HashSet<String>(Arrays.asList("hot","dot","dog","lot","log","cog"));
ArrayList<ArrayList<String>> res=findLadders(start, end, dict);
System.out.println(res);
// TODO Auto-generated method stub
}
}
在防止环产生时,用到的就是与根节点的距离:HashMap<String,Integer> distance.具体可以看下图
截图自:https://www.cnblogs.com/ShaneZhang/p/3748494.html ,这篇文章也比较好的说了思路