[Leetcode] Word Ladder II (Java)
来源:互联网 发布:阿里妈妈淘宝客怎么用 编辑:程序博客网 时间:2024/06/02 00:03
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["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 { static class Node { String word; ArrayList<Node> before; Node(String word) { this.word = word; this.before = new ArrayList<Node>(); } } public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) { ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>(); char[] chs = new char[start.length()]; ArrayList<Node> now = new ArrayList<Node>(), next = null; now.add(new Node(start)); dict.add(end); dict.remove(start); int length = start.length(), deep = 0; while (now.size() > 0 && dict.contains(end)) { deep++; next = new ArrayList<Node>(); HashMap<String, Node> nextMap = new HashMap<String, Node>(); for (Node i : now) { i.word.getChars(0, length, chs, 0); for (int j = 0; j < length; j++) { char tmp = chs[j]; for (char k = 'a'; k <= 'z'; k++) { chs[j] = k; String str = new String(chs); if (!dict.contains(str)) { continue; } Node node = nextMap.get(str); if (node == null) { node = new Node(str); nextMap.put(str, node); next.add(node); } node.before.add(i); } chs[j] = tmp; } } for (String key : nextMap.keySet()) { dict.remove(key); } now = next; } if (dict.contains(end)) { return ret; } Node root = null; for (Node i : now) { if (i.word.equals(end)) { root = i; break; } } Node[] nodes = new Node[deep + 2]; nodes[deep] = new Node(start); dfs(root, ret, nodes, 0, deep); return ret; } private void dfs(Node root, ArrayList<ArrayList<String>> ret, Node[] nodes, int nodeIndex, int deep) { if (nodeIndex == deep) { ArrayList<String> tmp = new ArrayList<String>(deep); for (int i = nodeIndex; i >= 0; i--) { tmp.add(nodes[i].word); } ret.add(tmp); } else { nodes[nodeIndex] = root; for (Node b : root.before) { dfs(b, ret, nodes, nodeIndex + 1, deep); } } }}
0 0
- [Leetcode] Word Ladder II (Java)
- [LeetCode][Java] Word Ladder II
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- [LeetCode] Word Ladder II
- Word Ladder II -- LeetCode
- Leetcode: Word Ladder II
- leetcode-Word Ladder II
- leetcode-Word Ladder II
- leetcode: Word Ladder II
- JMeter:使用正则表达式提取服务器动态返回数据
- oracle中对汉字跟英文字母进行混合排序 ,
- 设置收藏夹和首页的js代码
- 已Root的Android手机依然无法在DDMS里的File Explorer中访问data目录的解决方案
- 项目管理小结
- [Leetcode] Word Ladder II (Java)
- Linux系统中的jdk安装配置
- 线程/进程同步的方式和机制
- lua data structure
- 玉兔号苏醒了
- Hadoop 安装
- Debian中的rc.local .
- POJ 3592 Instantaneous Transference 图论算法tarjan+spfa
- 防止页面的重复提交,刷新