Leetcode——Word Ladder II

这是一道太难的题了,做了一天,最后进行一下记录和反思。

这道题主要是用到图的方法。包括邻接表构建图,然后用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 ,这篇文章也比较好的说了思路

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值