【LeetCode】127. Word Ladder
Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence 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"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
最短搜索路径,所以是广度优先搜索(BFS)。
有几个问题需要注意:
1、怎样判断是否为邻居节点?
按照定义,存在一个字母差异的单词为邻居,因此采用逐位替换字母并查找字典的方法寻找邻居。
(逐个比对字典里单词也可以做,但是在这题的情况下,时间复杂度会变高,容易TLE)
2、怎样记录路径长度?
对队列中的每个单词记录路径长度。从start进入队列记作1.
长度为i的字母的邻居,如果没有访问过,则路径长度为i+1
struct Node
{
string word;
int len; Node(string w, int l): word(w), len(l) {}
}; class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
queue<Node*> q;
unordered_map<string, bool> m;
Node* beginNode = new Node(beginWord, );
q.push(beginNode);
m[beginWord] = true;
while(!q.empty())
{
Node* frontNode = q.front();
q.pop();
string frontWord = frontNode->word;
//neighbor search
for(int i = ; i < frontWord.size(); i ++)
{
for(char c = 'a'; c <= 'z'; c ++)
{
if(c == frontWord[i])
continue; string frontWordCp = frontWord;
frontWordCp[i] = c;
//end
if(frontWordCp == endWord)
return frontNode->len+;
if(wordDict.find(frontWordCp) != wordDict.end() && m[frontWordCp] == false)
{
Node* neighborNode = new Node(frontWordCp, frontNode->len+);
q.push(neighborNode);
m[frontWordCp] = true;
}
}
}
}
return ;
}
};

【LeetCode】127. Word Ladder的更多相关文章
- 【LeetCode】127. Word Ladder 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
- 【leetcode】126. Word Ladder II
题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)
原题地址: https://oj.leetcode.com/submissions/detail/19446353/ 题目内容: Given two words (start and end), an ...
- 【LeetCode】79. Word Search
Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
随机推荐
- select点击option获取文本输入框的焦点事件
HTML文件: <select id="secOrderNum" style="margin-bottom:10px;width:90px;" data- ...
- easyui input设置为disabled提交后获取不到属性值
在做网站管理后台的用户修改功能时,由于当前用户修改个人信息时规定用户名不能修改,故使用了input标签的disabled属性,但是在提交数据后却发现用户名显示为空了.后 来一查才知道input设置为d ...
- 【BZOJ】【3671】【NOI2014】随机数生成器
贪心 嗯……其实生成这个矩阵就是一个$O(n^2)$的模拟 = = 然后?字典序最小?贪心呗= =能选1就选1,然后能选2就选2…… 我们发现,对于矩阵(1,1)~(n,m),假设1的位置是(x,y) ...
- 使用Java、Matlab画多边形闭合折线图
由于写论文要将“哈密顿回路问题(TSP)”的求解中间结果表示出来,查了一下使用程序画多边形图形.现在在总结一下,这个图是“由给定节点首尾相连的”闭合多边形. 1.使用matlab作闭合多边形图 没有找 ...
- js list数据 转 树状 层级 JSON,递归生成树状 层级 JSON
<!DOCTYPE html> <html> <head> <script> var data=[ {"id":"aaa& ...
- 【Hadoop】mapreduce采用多进程与spark采用多线程比较
转自:Mapreduce多进程与spark多线程 Apache Spark的高性能一定程度上取决于它采用的异步并发模型(这里指server/driver 端采用的模型),这与Hadoop 2.0(包括 ...
- 齐次坐标(Homogeneous Coordinates)
原文:http://blog.163.com/m_note/blog/static/208197045201272341230195/ 齐次坐标(Homogeneous Coordinates) 问题 ...
- 从Ubuntu12.04LTS到Foreda19再到Foreda8
装Ubuntu的初衷是以为它能识别我的PCI无线网卡,但装了两遍没有做到. 昨天在Ubuntu装jdk7,其过程与正常Linux安装jdk差别不小,有点背离的意思.另外VI的用法也和正常Unix/Li ...
- 【已解决】Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
转自:http://www.crifan.com/python_syntax_error_indentationerror/comment-page-1/ [问题] 一个python脚本,本来都运行好 ...
- 翻译记忆软件-塔多思TRADO经典教程_5
TRADOS新手必读 共有篇贴子 TRADOS新手必读 译海撷英系列之软件漫谈之TRADOS新手必读 作者:苏任 我很喜欢到翻译中国网站上来四处看看,一开始主要是关心应用资料和软件,后来就对网友的 ...