LeetCode刷题记188-Breadth First Search(广度优先搜索)

1 篇文章 0 订阅

126. 单词接龙 II

class Solution {
    int n;  // 单词个数 + 1(开头单词)
    int m;  // 单词长度
    List<Integer>[] g;  // 记录两个单词是否可以满足条件
    boolean[] v;
    int endInd = -1;  // 记录结束位置的下标

    public int diff(String word1, String word2) {  // 判断变换次数的函数
        int dif = 0;
        for (int i = 0; i < word1.length(); i ++) {
            if (word1.charAt(i) != word2.charAt(i)) dif ++;
        }
        return dif;
    }

    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        List<List<String>> ans = new ArrayList();
        n = wordList.size() + 1;
        m = endWord.length();
        g = new ArrayList[n];
        v = new boolean[n];

        for (int i = 0; i < n; i ++) {
            g[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < wordList.size(); i ++) {
            if (diff(beginWord, wordList.get(i)) == 1) {  // 和开头单词是否能够互通
                g[n - 1].add(i);
                g[i].add(n - 1);
            }
            if (diff(endWord, wordList.get(i)) == 0) endInd = i;
            for (int j = i + 1; j < wordList.size(); j ++) {
                if (diff(wordList.get(j), wordList.get(i)) == 1) {
                    g[j].add(i);
                    g[i].add(j);
                }
            }
        }
        if (endInd == -1) return ans;

        List<List<String>>[] path = new ArrayList[n];  // 记录到每个点的最短路径
        for (int i = 0; i < n; i ++) {
            path[i] = new ArrayList();
        }
        List<String> cur = new ArrayList<String>();
        cur.add(beginWord);
        path[n - 1].add(cur);

        int[] d = new int[n];  // 记录到每个点的最短距离
        Arrays.fill(d, Integer.MAX_VALUE);
        d[n - 1] = 0;

        Queue<Integer> q = new LinkedList<Integer>();
        q.add(n - 1);
        v[n - 1] = true;

        while (!q.isEmpty()) {
            int top = q.poll();
            if (d[top] >= d[endInd]) continue;
            for (int k = 0; k < g[top].size(); k ++) {
                int i = g[top].get(k);
                if (d[i] != Integer.MAX_VALUE && d[i] > d[endInd]) {
                    v[i] = true;
                    continue;
                }
                if (d[i] >= d[top] + 1) {  // 经过这个top这个点到i有最短路径
                    d[i] = d[top] + 1;
                    for (int j = 0; j < path[top].size(); j ++) {
                        cur = new ArrayList(path[top].get(j));
                        cur.add(wordList.get(i));
                        path[i].add(cur);
                    }
                }

                if (!v[i]) {
                    v[i] = true;
                    q.add(i);
                }
            }
        }
        return path[endInd];
    }
}

改进一点点,还是很慢

class Solution {
    int n;  // 单词个数 + 1(开头单词)
    int m;  // 单词长度
    List<Integer>[] g;  // 记录两个单词是否可以满足条件
    boolean[] v;
    int endInd = -1;  // 记录结束位置的下标
    List<List<String>> ans;
    List<Integer>[] pa;

    public int diff(String word1, String word2) {  // 判断变换次数的函数
        int dif = 0;
        for (int i = 0; i < word1.length(); i ++) {
            if (word1.charAt(i) != word2.charAt(i)) dif ++;
        }
        return dif;
    }

    public void findPath(List<String> wordList, List<String> cur, int fro, String beginWord) {
        // System.out.println(fro);
        if (fro == n - 1) {
            cur.add(beginWord);
            Collections.reverse(cur);
            ans.add(cur);
            return;
        }
        cur.add(wordList.get(fro));
        for (int i = 0; i < pa[fro].size(); i ++) {
            findPath(wordList, new ArrayList(cur), pa[fro].get(i), beginWord);
        }

    }

    public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
        ans = new ArrayList();
        n = wordList.size() + 1;
        m = endWord.length();
        g = new ArrayList[n];
        v = new boolean[n];

        for (int i = 0; i < n; i ++) {
            g[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < wordList.size(); i ++) {
            if (diff(beginWord, wordList.get(i)) == 1) {  // 和开头单词是否能够互通
                g[n - 1].add(i);
                g[i].add(n - 1);
            }
            if (diff(endWord, wordList.get(i)) == 0) endInd = i;
            for (int j = i + 1; j < wordList.size(); j ++) {
                if (diff(wordList.get(j), wordList.get(i)) == 1) {
                    g[j].add(i);
                    g[i].add(j);
                }
            }
        }
        if (endInd == -1) return ans;

        pa = new ArrayList[n];  // 记录每个点的父亲
        for (int i = 0; i < n; i ++) {
            pa[i] = new ArrayList<Integer>();
        }

        int[] d = new int[n];  // 记录到每个点的最短距离
        Arrays.fill(d, Integer.MAX_VALUE);
        d[n - 1] = 0;

        Queue<Integer> q = new LinkedList<Integer>();
        q.add(n - 1);
        v[n - 1] = true;

        while (!q.isEmpty()) {
            int top = q.poll();
            if (d[top] >= d[endInd]) continue;
            for (int k = 0; k < g[top].size(); k ++) {
                int i = g[top].get(k);
                if (d[i] != Integer.MAX_VALUE && d[i] > d[endInd]) {
                    v[i] = true;
                    continue;
                }
                if (d[i] >= d[top] + 1) {  // 经过这个top这个点到i有最短路径
                    d[i] = d[top] + 1;
                    pa[i].add(top);  // 给i点添加父亲
                }

                if (!v[i]) {
                    v[i] = true;
                    q.add(i);
                }
            }
        }
        findPath(wordList, new ArrayList(), endInd, beginWord);

        return ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值