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.
class node {
public:
string word;
int lv;
node(string s, int v): word(s), lv(v) {}
}; class Solution {
public: int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
int n = wordList.size(); if(!n) return ;
bool flag = false;
if(wordList.find(endWord) != wordList.end()) flag = true;
wordList.insert(endWord); queue<node> st;
st.push(node(beginWord, )); while(!st.empty()) {
node top = st.front();
st.pop();
int cur_lv = top.lv;
string cur_word = top.word; if(cur_word.compare(endWord) == ) return flag? cur_lv+: cur_lv;
unordered_set<string>::iterator p = wordList.begin(); for(int i=; i<cur_word.length(); ++i) {
for(char c = 'a'; c <= 'z'; ++c) {
char tmp = cur_word[i];
if(cur_word[i] != c) cur_word[i] = c;
if(wordList.find(cur_word) != wordList.end()) {
st.push(node(cur_word, cur_lv+));
wordList.erase(wordList.find(cur_word));
}
cur_word[i] = tmp;
}
}
} return ;
}
};

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. Bypass Preventing CSRF

    CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...

  2. nginx上用fastcgi配置python环境

    费了2天的功夫,翻阅了无数的中文.英文资料,终于搞定.写下此文留待以后翻阅用      本文环境,centOS 5.4 ,Nignx-0.8.49, Python 2.6.5   ========== ...

  3. Win32 DLL和MFC DLL 中封装对话框

    现在最常看见的关于DLL的问题就是如何在DLL中使用对话框,这是一个很普遍的关于如何在DLL中使用资源的问题.这里我们从Win32   DLL和MFC   DLL两个方面来分析并解决这个问题.     ...

  4. POJ 3904 Sky Code

    题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个? 分析:首先总共集合nCr(n, 4) = n*(n-1)*(n ...

  5. AAC ADTS AAC LATM 格式分析

    http://blog.csdn.net/tx3344/article/details/7414543# 目录(?)[-] ADTS是个啥 ADTS内容及结构 将AAC打包成ADTS格式 1.ADTS ...

  6. 【转】VC++消息钩子编程

    VC++消息钩子编程

  7. 对Delphi控件作用的新理解(控件本身的源代码就是一个很强的工业级源码)

    最近几天,对Delphi控件的含义有了一个新的理解.其实它不仅仅是给程序员提供功能的一个表层调用,控件本身的源代码就是一个很强的工业级源码.而且它的Main例子,往往就已经是半成品.而别的语言里没有那 ...

  8. ruby中Block, Proc 和 Lambda 浅析

    Block 与Proc的区别: Block是代码块,Proc是对象: 参数列表中最多只能有一个Block, 但是可以有多个Proc或Lambda; Block可以看成是Proc的一个类实例. Proc ...

  9. 【HDOJ】4374 One hundred layer

    线性DP,使用单调队列优化. /* 4374 */ #include <iostream> #include <sstream> #include <string> ...

  10. Android开发之全屏显示的两种方法

    1.通过修改清单文件中Theme,实现全屏 <application android:name=".MyApplication" android:allowBackup=&q ...