[leetcode]Word Ladder
此题要多品品。由于是求最短的路径,所以用BFS,DFS会在大数据的时候超时,原因也容易理解。最终解法参考了:http://discuss.leetcode.com/questions/1108/word-ladder 和 http://blog.sina.com.cn/s/blog_b9285de20101j1xl.html
1.用了char[] curr=queue.poll().toCharArray();这个在字符串中间替换字符时很有用;
2.用了thisLevel和nextLevel。本来用DFS的时候记录dist很自然,多一个递归dist++就行了。BFS的时候有点不知所措,现在知道了可以记录这一层的个数和下一层的个数。
public class Solution {public int ladderLength(String start, String end, HashSet<String> dict) {int dist = 1;if (start.equals(end)) return dist;HashSet<String> added = new HashSet<String>();Queue<String> queue = new LinkedList<String>();queue.offer(start);added.add(start);int thisLevel = 1;int nextLevel = 0;while (queue.size() != 0){char[] curr=queue.poll().toCharArray();thisLevel--;for (int i = 0; i < start.length(); i++){char tmp = curr[i];for (char c = 'a'; c <= 'z'; c++){if (c == tmp) continue;curr[i] = c;String str = new String(curr);if (str.equals(end)) return dist + 1;if (dict.contains(str) && !added.contains(str)){queue.offer(str);added.add(str);nextLevel++;}}curr[i] = tmp;}if (thisLevel == 0){thisLevel = nextLevel;nextLevel = 0;dist++;}}return 0;}
}
其实说白了,就是topo图的BFS,建个图更清晰,但也更麻烦一些。
http://blog.csdn.net/beiyetengqing/article/details/8580577
转载于:https://www.cnblogs.com/lautsie/p/3293547.html
[leetcode]Word Ladder相关推荐
- LeetCode: Word Ladder
直接看答案.. 1 class Solution { 2 public: 3 int ladderLength(string start, string end, unordered_set<s ...
- 【难点+重点BFS】LeetCode 126. Word Ladder II
LeetCode 126. Word Ladder II Solution1: 参考自花花酱:http://zxi.mytechroad.com/blog/searching/leetcode-126 ...
- 【重点BFS】LeetCode 127. Word Ladder
LeetCode 127. Word Ladder Solution1:我的超过40%的AC的答案 原先利用BFS做但是内存溢出未能AC:进过修改加上了标记是否访问过的visited数组,可以AC啦~ ...
- LeetCode 127. Word Ladder
原题链接在这里:https://leetcode.com/problems/word-ladder/ 题目: Given two words (beginWord and endWord), and ...
- Leetcode的word ladder问题
127. Word Ladder 题意如下: 给定一个开始字符串和结束字符串以及含有大量字符串的字典,要求通过一定规则,能从开始字符串变换到结束字符串,求其最小变换字符串数目,该规则如下: 1 从开始 ...
- PHP第五周答案,算法设计与分析第五周作业——Word Ladder
算法设计与分析第五周作业--Word Ladder 上周找了一道深度搜索优先搜索的算法题来做,于是这周就选了一道广度优先搜索算法题来试试手. 本周所选题目:原题目链接 题目详情 题目大意:给出一个字符 ...
- 算法细节系列(20):Word Ladder系列
算法细节系列(20):Word Ladder系列 详细代码可以fork下Github上leetcode项目,不定期更新. 题目摘自leetcode: 1. Leetcode 127: Word Lad ...
- Word Ladder
LeetCode[127. Word Ladder] 题解 难度[medium] 题目: Given two words (beginWord and endWord), and a dictiona ...
- LeetCode——Word Break
LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...
最新文章
- MySQL面试题 | 附答案解析(二十)
- Python 从入门到精通:一个月就够了?真的能行嘛?
- 在神经反馈任务中同时进行EEG-fMRI,多模态数据集成的大脑成像数据集
- C++ Bitsets
- 安卓之上传文件,即HTTP提交表单
- 炎炎夏日需要一个清凉的地 - 自制水冷系统(十一 指尖的思绪之程序篇)
- 2-06 使用网络存储SAN和NAS
- python合并表格用什么库_python合并多个excel表格数据库-python实现excel合并
- linux安装mysql 5.7_linux安装mysql5.7.24
- Kernel:CC_HAVE_ASM_GOTO 、 Compiler lacks asm-goto support
- 单体架构常用注解合集
- 厦门大学计算机专业录取分数线2019,2019厦门大学各省录取分数线是多少
- 半年内第二块国产全功能GPU,附带首个游戏显卡!摩尔线程造芯神速,张建中“黄”气逼人...
- 蒲丰投针计算机模拟ppt,蒲丰投针实验模课件.doc
- java基础案例 饲养员喂养动物
- 耿庆国 的旱震理论(可能这个理论不是他先提出来的)
- 【尚筹网项目】 三、【后台】 管理员信息维护
- Uncaught Error: USERKEY_PLAT_NOMATCH 的原因和解决办法
- Loadrunner 11安装教程
- java根据开始时间和结束时间计算中间间隔日期