LeetCode :Word Ladder II My Solution
Word Ladder II
Total Accepted: 11755 Total
Submissions: 102776My Submissions
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.
这个题目着有用了不少时间,中间做了两个取巧的处理:
1.把end放到dict里面这种优点就是在BFS建立图的时候 能够直接获取end的前继,全部.这样能够降低最后处理的麻烦
2.把dfs中处理进行优化,能够直接处理到当前继没有的时候,这个时候最后一个节点一定是start
3.中间建立一个MAP map中保存不同的str相应的前继
4.假设一个串被发现了,那么要看一下是不是distance和之前的str差一个,这个是为了保证不出现绕环
5.对str的每一位做变换,使时间复杂度下来.而刚開始的时候是比較有一个位不同样,可是对海量的处理的时候反而会减少速度.
6.对最后结果要把新的放在0位,oneAnswer.add(0,end).由于是从后往前扫
public class Solution {
public ArrayList<ArrayList<String>> findLadders(String start,
String end, HashSet<String> dict) {
dict.add(end);
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
if (start == null || end == null || start.length() != end.length()) {
return result;
}
Map<String, MyNode> map = new HashMap<String, MyNode>();
Queue<String> queue = new LinkedList<String>();
queue.offer(start);
map.put(start, new MyNode(start, 1));
while (!queue.isEmpty()) {
String cur = queue.poll();
if (reachEnd(cur, end)) {
outputResult(end, new ArrayList<String>(), map, result);
return result;
}
for (int i = 0; i < cur.length(); i++) {
for (char c = 'a'; c <= 'z'; c++) {
String changeStr = getOneDiff(cur, i, c);
if (dict.contains(changeStr)) {
MyNode curNode = map.get(cur);
int curDist = curNode.distance;
int newDist = curDist + 1;
if (!map.containsKey(changeStr)) {
MyNode newNode = new MyNode(changeStr, newDist);
newNode.pre.add(curNode);
queue.offer(changeStr);
map.put(changeStr, newNode);
} else {
MyNode preNode = map.get(changeStr);
int preDist = preNode.distance;
if (newDist == preDist) {
preNode.pre.add(curNode);
}
}
}
}
}
}
return result;
}
private void outputResult(String end, ArrayList<String> oneAnswer,
Map<String, MyNode> map, ArrayList<ArrayList<String>> result) {
MyNode curNode = map.get(end);
oneAnswer.add(0, end);
if (curNode.pre.isEmpty()) {
result.add(new ArrayList<String>(oneAnswer));
return;
}
for (MyNode eachNode : curNode.pre) {
outputResult(eachNode.val, oneAnswer, map, result);
oneAnswer.remove(0);
}
}
private void getPaths(MyNode myNode, Map<String, MyNode> map,
ArrayList<String> curPath, ArrayList<ArrayList<String>> paths) {
if (myNode == null) {
paths.add(curPath);
return;
}
curPath.add(0, myNode.val);
if (!myNode.pre.isEmpty()) {
for (MyNode prevNode : myNode.pre) {
getPaths(prevNode, map, new ArrayList<String>(curPath), paths);
}
} else {
getPaths(null, map, curPath, paths);
}
}
boolean reachEnd(String left, String right) {
if (left.equals(right)) {
return true;
}
return false;
}
String getOneDiff(String str, int pos, char c) {
StringBuffer sb = new StringBuffer(str);
sb.setCharAt(pos, c);
return sb.toString();
}
}
class MyNode {
String val;
int distance;
LinkedList<MyNode> pre;
MyNode(String v, int distance) {
this.val = v;
this.distance = distance;
pre = new LinkedList<MyNode>();
}
void addPre(MyNode p) {
pre.add(p);
}
}
不得不说,这个题做的确实痛苦,可是做AC了之后感觉非常爽!
也不知道各位大神有什么更好的办法,反正我做的时候确实感觉到非常挑战,但非常有意思.
过瘾!
LeetCode :Word Ladder II My Solution的更多相关文章
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- leetcode—word ladder II
1.题目描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence( ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
随机推荐
- N 组连续子串最大和
数组 a 中有 M 个数 , 将 M 个数分成 N 组 , 并且每组中的数据顺序和原数组中的顺序保持一致,求 N 组中的数据之和最大为多少? 向 dp 数组中赋初始值 ,如果 M == N ,则 dp ...
- poj 1321 棋盘问题 递归运算
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19935 Accepted: 9933 Description ...
- 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程
想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...
- javascript小练习—点击将DIV变成红色(通过for循环遍历)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- table操作:边框-斑马线-多表头-焦点高亮-自动求和
一.操作table,本例子实现的功能: 1.table等宽边框2.table斑马线3.实现table多表头4.焦点所在行高亮5.自动计算总分 二.效果图 三.代码: <!DOCTYPE html ...
- 第三条:私有化构造器或者枚举类型强化Singleton属性
1.5版本之前,我们通常实现单例模式有两种方式: 两种方法前提都是私有化构造器,然后通过不同的方式获取对象. 第一种:通过公共的静态变量获取 public class Elivs{ // 私有化构造器 ...
- hdu 4612 Warm up 有重边缩点+树的直径
题目链接 Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tot ...
- IOS 特定于设备的开发:使用加速器启动屏幕上的对象
借助一点编程工作,iPhone的机载加速计就可以使对象在屏幕上四处“移动”,实时响应用户倾斜手机的方式.下面的代码就是创建一个动画式的蝴蝶,用户可以使之快速移过屏幕. 使之工作的秘密在于:向程序中添加 ...
- ios 刷新BUG
UItextView edited = yes ; 导致 刷新奇怪: 有些方法不要自己调用: 可能会破坏组件自己的生命周期: 建议模块化开发组件,自己处理自己的事:
- SQL 时间戳
一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一个时间字段,每次增加数据时,填入当前的时间值.其实这误导了很多朋友. 1.基本概念 时间戳:数据库中自动生成的唯一二进制数字,与时间 ...