127. 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: Only one letter can be changed at a time.
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example, Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length . Note:
Return if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.
UPDATE (//):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
  • Difficulty:Medium
  • Total Accepted:129.6K
  • Total Submissions:667.5K
  • Contributor:LeetCode
 class Solution {
public:
bool cmpwrd(string str1, string str2)
{
size_t strlen = str1.size();
int cnt = ;
for (int i = ; i < strlen; ++i)
{
if(str1[i] == str2[i]) ++cnt;
}
if(cnt == (strlen - )) return true;
return false;
} int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
if(find(wordList.begin(), wordList.end(), endWord) == wordList.end()) return ;
size_t wLlen = wordList.size();
vector<bool> visited(wLlen, false);
int sum = ;
queue<string> qs;
qs.push(beginWord);
qs.push("#"); // level flag while (!qs.empty())
{
string tmpstr = qs.front();
qs.pop();
if(tmpstr == "#")
{
if(!qs.empty())
{
qs.push(tmpstr);
tmpstr = qs.front();
qs.pop();
++sum;
}
else return ;
} if(tmpstr == endWord) return sum; //seek for all the possible next node
for (int j = ; j < wLlen; ++j)
{
if(!visited[j] && cmpwrd(tmpstr, wordList[j]))
{
if(tmpstr == endWord)
{
//cout << wordList[j] << "\n" << endWord << endl;
return (++sum);
}
//cout << wordList[j] << " ";
visited[j] = true;
qs.push(wordList[j]);
}
}
//cout << endl;
}
return sum;
}
};

12.21%  Runtime: 806 ms 39 / 39 test cases passed

 class Solution {  //by kaishen2
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordDict) {
unordered_set<string> word_dict_;
for (auto &word : wordDict) word_dict_.insert(word);
if (word_dict_.find(endWord) == word_dict_.end()) return ;
else word_dict_.erase(endWord);
unordered_set<string> q1, q2, temp;
q1.insert(beginWord);
q2.insert(endWord);
int count = ;
while (!q1.empty() && !q2.empty()) {
++count;
if (q1.size() > q2.size()) swap(q1, q2);
temp.clear();
for (auto word_ : q1) {
for (int j = ; j < word_.size(); ++j) {
char hold = word_[j];
for (char i = 'a'; i <= 'z'; ++i) {
word_[j] = i;
if (q2.find(word_) != q2.end()) return count + ;
if (word_dict_.find(word_) != word_dict_.end()) {
word_dict_.erase(word_);
temp.insert(word_);
}
}
word_[j] = hold;
}
}
swap(q1, temp);
}
return ;
}
};

93.07% 35ms

127. Word Ladder(M)的更多相关文章

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

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

  2. Leetcode#127 Word Ladder

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

  3. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

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

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

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

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

  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. 127 Word Ladder

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

  8. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

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

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

随机推荐

  1. 对C++多态的一点理解

    作为一个C++新手,我浅谈一下我对多态的一点理解. 首先,引用几句话: 1.继承是一种抽象,它允许程序员在某些时候忽略相似对象的差异,又在其他时候利用这些差异.——<C++沉思录> 2.继 ...

  2. mfc Unicode转 ASNI ,WCHAR 转 CHAR

    知识点: 宽字符转多字节字符 多字节字符转宽字符 什么是ANSI,什么又是UNICODE呢?其实这是两种不同的编码方式标准,ANSI中的字符采用8bit,而UNICODE中的字符采用16bit 在VC ...

  3. JQuery快速入门-Ajax

    一.AJAX概述 概念:AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). 优点:通过在后台与服务器进行少量数据交换,AJAX ...

  4. shellcode 编码技术

    在很多漏洞利用场景中, shellcode 的内容将会受到限制. 例如你不能输入 \x00 这个字符,编辑框不能输入 \x0d \x0a这样的字符 所以需要完成 shellcode 的逻辑,然后使用编 ...

  5. 做游戏的小伙伴们注意了,DDoS还可以这样破!

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 作者:腾讯DDoS安全专家.腾讯云游戏安全专家haroldchen 摘要:在游戏出海的过程中,DDoS攻 ...

  6. float和position的使用

    http://blog.csdn.net/yaodebian/article/details/58621183

  7. .net中操作Visual SourceSafe

    最近整理一些资料,发现以前写的一段代码,提供对微软的版本管理软件visual sourcesafe的一些操作.以下简称vss. 想起以前写的时候,因为资料比较匮乏,只能边研究边测试,走了不少弯路. 由 ...

  8. Linux内核实验作业六

    实验作业:分析Linux内核创建一个新进程的过程 20135313吴子怡.北京电子科技学院 [第一部分]阅读理解task_struct数据结构 1.进程是计算机中已运行程序的实体.在面向线程设计的系统 ...

  9. 20135316王剑桥Linux内核学习笔记第三周

    20135316王剑桥 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC 1000029000 三个法宝:存储程序计算机.函数调 ...

  10. ElasticSearch 2 (14) - 深入搜索系列之全文搜索

    ElasticSearch 2 (14) - 深入搜索系列之全文搜索 摘要 在看过结构化搜索之后,我们看看怎样在全文字段中查找相关度最高的文档. 全文搜索两个最重要的方面是: 相关(relevance ...