https://leetcode.com/problems/word-ladder/

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.
  1. class node {
  2. public:
  3. string word;
  4. int lv;
  5. node(string s, int v): word(s), lv(v) {}
  6. };
  7.  
  8. class Solution {
  9. public:
  10.  
  11. int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
  12. int n = wordList.size();
  13.  
  14. if(!n) return ;
  15. bool flag = false;
  16. if(wordList.find(endWord) != wordList.end()) flag = true;
  17. wordList.insert(endWord);
  18.  
  19. queue<node> st;
  20. st.push(node(beginWord, ));
  21.  
  22. while(!st.empty()) {
  23. node top = st.front();
  24. st.pop();
  25. int cur_lv = top.lv;
  26. string cur_word = top.word;
  27.  
  28. if(cur_word.compare(endWord) == ) return flag? cur_lv+: cur_lv;
  29. unordered_set<string>::iterator p = wordList.begin();
  30.  
  31. for(int i=; i<cur_word.length(); ++i) {
  32. for(char c = 'a'; c <= 'z'; ++c) {
  33. char tmp = cur_word[i];
  34. if(cur_word[i] != c) cur_word[i] = c;
  35. if(wordList.find(cur_word) != wordList.end()) {
  36. st.push(node(cur_word, cur_lv+));
  37. wordList.erase(wordList.find(cur_word));
  38. }
  39. cur_word[i] = tmp;
  40. }
  41. }
  42. }
  43.  
  44. return ;
  45. }
  46. };

leetcode@ [127] Word Ladder (BFS / Graph)的更多相关文章

  1. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  4. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  5. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  6. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  7. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. Java for LeetCode 127 Word Ladder

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

  9. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

随机推荐

  1. unity3d与eclipse集成开发android应用

    原地址:http://blog.csdn.net/armoonwei/article/details/7032537 Unity as a Library Once you have eclipse ...

  2. python参考手册--第2章词汇和语法约定

    1.续行符\ 三引号.().{}.[]中的内容不需要续行符 2.空格缩进 优选空格作为缩进,不要用tab,这是因为不同操作系统下tab对应的空格不一样,而python是通过严格的空格来控制语句块的. ...

  3. Pie Charts

    Default pie chart   The default pie chart with no options set. Source Code $.plot('#placeholder', da ...

  4. 1030-ACM程序设计之马拉松竞赛

    描述 校ACM协会近四个月举行了为期100天ACM程序设计之马拉松竞赛,竞赛题总数为1000,同学们反响热烈,先后有许多ACM程序设计竞赛爱好者开始先后编号,成功解答的题目数为选手的成绩. 今天进行成 ...

  5. POJ3096Surprising Strings(map)

    题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...

  6. PHP开篇之环境的搭建

    PHP开篇之环境的搭建 Wamp软件下载:http://www.wampserver.com/ 此时是2.5版本 下载下来一键安装. 安装有个主意 这里先不用管 或者smtp@qq.com 13643 ...

  7. 万网空间如何安装wordpress

    万网空间如何安装wordpress建站教程 _ 学做网站论坛 http://www.xuewangzhan.com/wpbbs/1643.html   1.先在本地下载一个最新版本的wordpress ...

  8. 好用的linux命令

    sudo chown -R `whoami` /usr/local # ps aux |grep php-fpm php-frm start and stop php-fpm -D killall p ...

  9. Android 九宫格密码锁进入程序

    设置九宫格密码锁进入程序,设置,重置,取消等,安卓巴士地址http://www.apkbus.com/forum.php?mod=viewthread&tid=182620&extra ...

  10. Android开发经验记录

    一.    代码规范 定一个规范的主要目的,是为了让不同的开发人员写的代码能保持一致性,方便别人看自己的代码.另外,对个人来说,也能起到让自己看着舒服的作用. 1.      基本 * 使用UTF-8 ...