本文主要是介绍leetcode----127. Word Ladder,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
链接:
https://leetcode.com/problems/word-ladder/
大意:
给定一个单词beginWord以及单词endWord,还有一个词典wordList。要求找出从beginWord转换为endWord的最短序列长度,且每次转换候的单词都必须是wordList中单词,且每次转换只能是两个仅有一位(且是同一位置)不同的字符串进行转换。例子:
思路:
dfs回溯+剪枝。
从endWord往beginWord进行回溯,最终超时...
请看zhazhad代码。
代码:(超时)
class Solution {int minCount = Integer.MAX_VALUE;int curCount = 1; // 初始为1public int ladderLength(String beginWord, String endWord, List<String> wordList) {if (!wordList.contains(endWord))return 0;// 一次转换即可 转换序列长度为2if (oneWordDifferent(beginWord, endWord))return 2;// 从endWord开始dfs逆推表演 将endWord的访问标志置为trueboolean[] visited = new boolean[wordList.size()];for (int i = 0; i < wordList.size(); i++) {if (wordList.get(i).equals(endWord)) {visited[i] = true;break;}}dfs(beginWord, endWord, wordList, visited);return minCount == Integer.MAX_VALUE ? 0 : minCount;}public void dfs(String beginWord, String curWord, List<String> wordList, boolean[] visited) {// System.out.println(beginWord + ":" + curWord + ":" + curCount);if (oneWordDifferent(beginWord, curWord)) {minCount = curCount + 1;return ;}curCount++;int idx = 0;// 剪枝while (curCount < minCount && idx < wordList.size()) {if (!visited[idx] && oneWordDifferent(curWord, wordList.get(idx))) {visited[idx] = true;dfs(beginWord, wordList.get(idx), wordList, visited);visited[idx] = false; // 回溯}idx++;}curCount--;}// 判断两个单词是否只有对应一位不同public boolean oneWordDifferent(String beginWord, String curWord) {int c = 0, idx = 0;while (idx < beginWord.length()) {if (beginWord.charAt(idx) != curWord.charAt(idx))c++;idx++;}return c == 1;}
}
结果:
超时。思考:也许得用BFS。。。
改进:
使用广度优先遍历算法解决。虽然通过了,但是效率好低啊(蠢哭... 急需大神代码安慰
class Solution {public int ladderLength(String beginWord, String endWord, List<String> wordList) {if (!wordList.contains(endWord))return 0;// 一次转换即可 转换序列长度为2if (oneWordDifferent(beginWord, endWord))return 2;boolean[] visited = new boolean[wordList.size()];int count = 1;for (int i = 0; i < wordList.size(); i++) {if (wordList.get(i).equals(endWord)) {visited[i] = true;break;}}List<String> curWords = new ArrayList<>();curWords.add(endWord);while (curWords.size() > 0) {List<String> tmp = new ArrayList<>();for (String s : curWords) {for (int i = 0; i < wordList.size(); i++) {if (!visited[i] && oneWordDifferent(s, wordList.get(i))) {tmp.add(wordList.get(i));visited[i] = true;// 快速判断if (wordList.get(i).equals(beginWord))return count + 1;if (oneWordDifferent(wordList.get(i), beginWord))return count + 2;}}}count += 1;curWords = tmp;}return 0;}// 判断两个单词是否只有对应一位不同public boolean oneWordDifferent(String beginWord, String curWord) {int c = 0, idx = 0;while (idx < beginWord.length()) {if (beginWord.charAt(idx) != curWord.charAt(idx))c++;idx++;}return c == 1;}
}
最佳:
class Solution {public int ladderLength(String beginWord, String endWord, List<String> wordList) {if (beginWord == null || endWord == null || wordList == null) {return 0;}// 将list转为set 查找速度转为O(1)Set<String> dict = new HashSet<>(wordList);if (!dict.contains(endWord)) {return 0;}Set<String> set1 = new HashSet<>();Set<String> set2 = new HashSet<>();set1.add(beginWord);set2.add(endWord);return bfs(set1, set2, dict, 1);}private int bfs(Set<String> set1, Set<String> set2, Set<String> dict, int len) {// 确保每次bfs都是对含元素少的set进行bfsif (set1.size() > set2.size()) {return bfs(set2, set1, dict, len);}Set<String> nextSet = new HashSet<>();for (String word : set1) {char[] chs = word.toCharArray();for (int i = 0; i < chs.length; i++) {char oldChar = chs[i];for (char c = 'a'; c <= 'z'; c++) {// 依次修改chs的每个位置上的字母(改为'a'-'z') 查看set2是否含有新单词if (c != oldChar) {chs[i] = c;}String newWord = new String(chs);if (set2.contains(newWord)) {return len + 1;}if (dict.contains(newWord)) {nextSet.add(newWord);dict.remove(newWord);}}chs[i] = oldChar; // 将chs[i]修改为原来的字母 下一步修改下一位置的字母}}// nextSet为空表明set1中所有元素都转不成dict中的字符串if (nextSet.isEmpty()) {return 0;}return bfs(nextSet, set2, dict, len + 1);}
}
结论:
看着大神写的代码,就是心旷神怡。(本菜鸡还是得多联系...
这篇关于leetcode----127. Word Ladder的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!