leetcode 126. 单词接龙 II bfs+dfs

10 篇文章 0 订阅

给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列。转换需遵循如下规则:
每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:
如果不存在这样的转换序列,返回一个空列表。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。

思路:bfs寻找最小值,顺便记录当前节点的前置节点,bfs跑一遍即可。
public class __126 {
    public boolean check(String s1, String s2) {
        int diff=0;
        for (int i=0;i<s1.length();++i) {
            if (s1.charAt(i)!=s2.charAt(i)) {
                if(++diff>1){
                    return false;
                }
            }
        }
        return diff==1;
    }
    public List<List<String>> res = new ArrayList<List<String>>();
    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList){
        Queue<Integer> queue = new LinkedList<Integer>();
        Collections.sort(wordList); //排序,样例结果是从大到小排序
        int st=Collections.binarySearch(wordList,beginWord);
        int ed=Collections.binarySearch(wordList,endWord);
        if(st<0){
            wordList.add(beginWord);
            st=wordList.size()-1;
        }
        if(ed<0){
            return new ArrayList<List<String>>();
        }
        Map<Integer,List<Integer>> fa=new HashMap<Integer,List<Integer>>();//记录父亲节点 上一层的节点
        for(int i=0;i<=wordList.size();++i){
            fa.put(i,new ArrayList<Integer>());
        }
        int[] hi=new int[wordList.size()]; //记录当前的长度
        hi[st]=1; //初始长度为1
        queue.offer(st);
        while (!queue.isEmpty()) {
            int idx=queue.poll();
            if(hi[ed]!=0&&hi[idx]>=hi[ed]){
                break;
            }
            for(int i=wordList.size()-1;i>=0;--i){
                //若层数为0或者重复访问过的节点
                if((hi[i]==0||hi[idx]+1==hi[i])&&check(wordList.get(idx),wordList.get(i))){
                    if(hi[i]==0){
                        hi[i]=hi[idx]+1;
                        fa.get(i).add(idx);
                        queue.add(i);
                    }else{
                        fa.get(i).add(idx);
                    }
                }
            }
        }
        if(hi[ed]==0){
           return new ArrayList<List<String>>();
        } else {
            Stack<String> s=new Stack<String>();
            s.add(endWord);
            dfs(fa,ed,hi[ed]-1,s,wordList);
        }
        return res;
    }
    public void dfs(Map<Integer,List<Integer>> fa,int idx,int k,Stack<String> st,List<String> wordList){
        if(k==0){
            List<String> list=new ArrayList<String>(st);
            Collections.reverse(list);
            res.add(list);
            return;
        }
        for(int id:fa.get(idx)){
            st.add(wordList.get(id));
            dfs(fa,id,k-1,st,wordList);
            st.pop();
        }
    }
    public static void main(String[] args) {
        String b="hit";
        String e="cog";
        List<String> wd=new ArrayList<String>(Arrays.asList("hot","dot","dog","lot","log","cog"));
        System.out.println(new __126().findLadders(b,e,wd));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值