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. WebUI框架

    Telerik Kendo UI http://demos.telerik.com/kendo-ui/window/right-to-left-support

  2. RASP 完爆 WAF 的5大理由!

    Web 应用防火墙(WAF)已经成为常见 Web 应用普遍采用的安全防护工具,即便如此,WAF 提供的保护方案仍旧存在诸多不足,笔者认为称 WAF 为好的安全监控工具更为恰当.幸运的是,应用安全保护技 ...

  3. hdu 2897 邂逅明下 博弈论

    这个分区间: 1<=n%(p+q)<=p 必败 其他必胜! 代码如下: #include<iostream> #include<stdio.h> #include& ...

  4. [itint5]棋盘漫步

    要注意dp[0][0]要初始化为1. int totalPath(vector<vector<bool> > &blocked) { int m = blocked.s ...

  5. [itint5]三数和为0

    http://www.itint5.com/oj/#20 其实是3sum的变种,有重复数字,但是一开始还是写错了.其实是选定一个后,在右边剩余数组里找2sum,找到一组后继续找. #include & ...

  6. Revit 二次开发 沿弧形路径创建拉伸屋顶

    沿弧形路径创建拉伸屋顶 Revit的API中只能按照直线创建拉伸屋顶,不能按照曲线创建拉伸屋顶.在Revit的界面当中,可以用 构建->内建模型,进行放样创建屋顶.但是没有办法代码内建模型. 可 ...

  7. 【算法】 输入n 输出一个n*n的zigzag矩阵 利用c++实现

    int main() { int N; cin>>N; int **a = new int *[N]; ) ;//如果没有申请到空间 ;i<N;i++) { a[i]= new in ...

  8. Android 签名(7)签名常见问题,debug签名和release签名的区别等

    一般在安装时提示出错:INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 1) 两个应用,名字相同,签名不同 2) 升级时前一版本签名,后一版本没签名 3) ...

  9. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  10. php discuz框架接口不能正常访问的问题

    本人php小白,无php编程基础,直接上php服务器部署,后果很严重.....所以务必看完请给”顶“给评论,以表示对小白的鼓励和赞赏! 关于discuz框架,独自加班,废寝忘食,然已无力吐槽..... ...