题目链接

Word Ladder - LeetCode

注意点

  • 每一个变化的字母都要在wordList中

解法

解法一:bfs。类似走迷宫,有26个方向(即26个字母),起点是beginWord,终点是endWord。每一次清空完队列ret+1表示走了一步。bfs可以保证是最短路径。

class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_set<string> wordSet(wordList.begin(),wordList.end());
if(!wordSet.count(endWord)) return 0;
queue<string> q;
q.push(beginWord);
int ret = 0;
while(!q.empty())
{
int size = q.size();
for(int i = 0;i < size;i++)
{
string word = q.front();q.pop();
if(word == endWord) return ret+1;
for(int j = 0;j < word.size();j++)
{
string newWord = word;
for(char ch = 'a';ch <= 'z';ch++)
{
newWord[j] = ch;
if(wordSet.count(newWord) && newWord != word)
{
q.push(newWord);
wordSet.erase(newWord);
}
}
}
}
ret++;
}
return 0;
}
};

小结

  • 不看别人的解题报告真想不到是迷宫问题

Word Ladder - LeetCode的更多相关文章

  1. Word Ladder——LeetCode

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  2. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  3. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  4. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  6. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  7. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  8. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  9. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

随机推荐

  1. [Spark][Python]Spark Join 小例子

    [training@localhost ~]$ hdfs dfs -cat people.json {"name":"Alice","pcode&qu ...

  2. C#抽象类跟接口

    抽象类描述的是一个什么东西,属性. 抽象类是对类的抽象,描述是什么  抽象类,继承后重写接口描述的是他做什么,行为.接口是对行为的抽象,描述做什么  ,进行继承后实行接口

  3. python基础学习笔记(十一)

    迭代器 本节进行迭代器的讨论.只讨论一个特殊方法---- __iter__  ,这个方法是迭代器规则的基础. 迭代器规则 迭代的意思是重复做一些事很多次---就像在循环中做的那样.__iter__ 方 ...

  4. JavaScript术语:shim 和 polyfill

    转自:https://www.html.cn/archives/8339 在学习和使用 JavaScript 的时候,我们会经常碰到两个术语:shim 和 polyfill.它们有许多定义和解释,意思 ...

  5. 20135337——Linux实践二:模块

    一.编译&生成&测试&删除 1.编写模块代码,查看如下 gedit 1.c(编写) cat 1.c(查看) MODULE_AUTHOR("Z") MODUL ...

  6. git 使用ssh密钥

    一.查看仓库支持的传输协议 1.1查看仓库支持的传输协议 使用命令 git remote -v 查看你当前的 remote url root@zengyue:/home/yuanGit# git re ...

  7. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  8. octave基本指令4

    octave基本指令4 图形化显示数据 >> t=[0:0.01:0.98]; >> y1 = sin(2*pi*4*t); %pi表示π >> plot(t,y1 ...

  9. 业务-----修改Service常用逻辑

    注意:修改时唯一属性不能重复 //num==null 时,没有修改Num,不用考虑重复问题.//num!=null 时,修改了num.考虑重复问题 if(!StringUtils.isEmpty(re ...

  10. AWK学习一例

    awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }'