LeetCode 126. Word Ladder II(单词梯子)

原题网址:https://leetcode.com/problems/word-ladder-ii/

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

方法一:广度优先搜索,递推实现。

public class Solution {
    public List> findLadders(String beginWord, String endWord, Set wordList) {
        List> ladders = new ArrayList<>();
        Map> maskedWords = new HashMap<>();
        wordList.add(beginWord);
        wordList.add(endWord);
        for(String word: wordList) {
            char[] wa = word.toCharArray();
            for(int i=0; i words = maskedWords.get(mask);
                if (words == null) {
                    words = new ArrayList<>();
                    maskedWords.put(mask, words);
                }
                words.add(word);
            }
        }
        wordList.remove(beginWord);
        List currents = new ArrayList<>();
        currents.add(new Ladder(beginWord, null));
        int from = 0;
        int to = 1;
        BFS: while (from < currents.size()) {
            to = currents.size();
            for(int i=from; i words = maskedWords.get(mask);
                    for(String w: words) {
                        if (wordList.contains(w)) {
                            currents.add(new Ladder(w, currents.get(i)));
                        }
                    }
                }
            }
            from = to;
            for(int i=to; i ladder = new LinkedList<>();
                Ladder l = currents.get(i);
                do {
                    ladder.addFirst(l.word);
                    l = l.prev;
                } while (l != null);
                ladders.add(ladder);
            }
        }
        return ladders;
    }
}
class Ladder {
    String word;
    Ladder prev;
    Ladder(String word, Ladder prev) {
        this.word = word;
        this.prev = prev;
    }
}

方法二:广度优先搜索,关键是搜索完整一层之后,再删除相关节点。

public class Solution {
    private List> find(List ladders, String endWord, Set wordList) {
        List> results = new ArrayList<>();
        for(Ladder ladder : ladders) {
            if (ladder.word.equals(endWord)) {
                List result = new ArrayList<>();
                while (ladder != null) {
                    result.add(0, ladder.word);
                    ladder = ladder.prev;
                }
                results.add(result);
            }
        }
        if (!results.isEmpty()) return results;
        
        List nexts = new ArrayList<>();
        for(Ladder ladder : ladders) {
            char[] la = ladder.word.toCharArray();
            for(int i=0; i();
        for(Ladder ladder : nexts) wordList.remove(ladder.word);
        return find(nexts, endWord, wordList);
    }
    public List> findLadders(String beginWord, String endWord, Set wordList) {
        wordList.remove(beginWord);
        List ladders = new ArrayList<>();
        ladders.add(new Ladder(beginWord, null));
        return find(ladders, endWord, wordList);
    }
}
class Ladder {
    String word;
    Ladder prev;
    Ladder(String word, Ladder prev) {
        this.word = word;
        this.prev = prev;
    }
}


你可能感兴趣的:(最短路径,路径,广度优先搜索,递归)