这道题可以考虑两种方式解决
1. 深度优先遍历
2. 广度优先遍历
其中算法的效率取决于数组中单词的顺序,如果解决方案靠前,那么深度优先所用时间短;如果解在中间的位置,那么广度优先用时较短
深度优先采用回溯递归调用的方式
public int ladderLengthTraceback(String start, String end, Set<String> dict) { List<String> words = new ArrayList<String>(); List<String> result = new ArrayList<String>(); backtrack(start, end, dict, words, result); if(result.isEmpty()){ return 0; } return result.size()+1; } private void backtrack(String start, String end, Set<String> dict, List<String> words, List<String> result){ if(start.equals(end)){ if(result.isEmpty() || words.size() < result.size()){ result.clear(); result.addAll(words); } return; }else if (!result.isEmpty() && words.size()>= result.size()){ return; } List<String> cw = new ArrayList<String>(); for(int i=0; i<start.length(); i++){ char[] startCharArray = start.toCharArray(); for(int j=0; j<26; j++){ startCharArray[i] = (char)('a' +j); String word = new String(startCharArray); if((dict.contains(word) && !words.contains(word)) || word.equals(end)){ cw.add(word); } } } for(String word : cw){ int size = words.size(); words.add(word); backtrack(word, end, dict, words, result); words.remove(size); } }
广度优先采用队列循环的方式
public int ladderLength(String start, String end, Set<String> dict){ if (start == null || end == null || start.equals(end) || start.length() != end.length()) return 0; if (isOneWordDiff(start, end)) return 2; Queue<String> queue=new LinkedList<String>(); HashMap<String,Integer> dist=new HashMap<String,Integer>(); queue.add(start); dist.put(start, 1); while(!queue.isEmpty()) { String head=queue.poll(); int headDist=dist.get(head); //从每一个位置开始替换成a~z for(int i=0;i<head.length();i++) { for(char j='a';j<'z';j++) { if(head.charAt(i)==j) continue; char[] s = head.toCharArray(); s[i]=j; String sb = new String(s); if(sb.toString().equals(end)) return headDist+1; if(dict.contains(sb.toString())&&!dist.containsKey(sb.toString())) { queue.add(sb.toString()); dist.put(sb.toString(), headDist+1); } } } } return 0; } private boolean isOneWordDiff(String a, String b) { int diff = 0; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) != b.charAt(i)) { diff++; if (diff >= 2) return false; } } return diff == 1; }
相关推荐
public void helper(TreeNode root, int level){// 当前层没有 list,新建// 取得当前层的 list迭代pub
javascript js_leetcode题解之126-word-ladder-ii.js
本文将详细解析三个使用 Java 实现的 LeetCode 题目的解法,主要涉及双指针技巧的应用。 1. **有序数组的 Two Sum** (LeetCode 167) 这个问题是要求在一个有序数组中找到两个数,使得它们的和等于目标值 `target`...
总之,"javalruleetcode-leetcode-algorithms-java" 是一个专注于 Java 语言和 LeetCode 算法的宝贵资源,它不仅提供了 LRUCache 的实现,还包括了广泛的算法题目的解答。通过深入研究这个项目,开发者不仅可以学习...
5. 深度优先搜索(DFS)和广度优先搜索(BFS):图遍历方法,用于解决路径寻找、连通性等问题。 项目中的"AlgorithmsInJava-master"很可能是一个源代码仓库,包含了上述所有概念的Java实现。通过阅读和学习这些代码...
java java_leetcode-115-distinct-subsquences
java java_leetcode-101-symmetric-tree
java java_leetcode-100-same-tree
java java_leetcode-73-set-matrix-zeroes
java java_leetcode-112-path-sum
java java_leetcode-110-balanced-binary-tree
java lru leetcode LeetCode-Tag-Java 解决方案 LeetCode 的解决方案 指数
java java_leetcode-maximum-depth-of-binary-tree
java java_leetcode-99-recover-binary-search-tree
java java_leetcode-113-path-sumII
java java_leetcode-111-minimum-depth-of-binary-tree
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
java java_leetcode-114-flatten-binary-tree-to-linked-list
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde