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. java maven项目迁移时缺失jar包 或者 maven jar包缺失时的解决方案

    这样弄完,jar包就都下载好了,就不缺失了. 从GitHub上checkout一个项目下来,导入idea后发现加载依赖奇慢无比,所以临时把网络调成FQ的代理,结果会发现idea会停止之前的下载,那怎么 ...

  2. [LOJ#6033]. 「雅礼集训 2017 Day2」棋盘游戏[二分图博弈、匈牙利算法]

    题意 题目链接 分析 二分图博弈经典模型,首先将棋盘二分图染色. 考虑在某个最大匹配中: 如果存在完美匹配则先手必败,因为先手选定的任何一个起点都在完美匹配中,而后手则只需要走这个点的匹配点,然后先手 ...

  3. 我的SQL SERVER数据库会装满吗?

    概述 今天有个客户问我一个蛮有意思的问题.我使用的SQL SERVER 2008数据库,目前数据库130多G,其中某个表的记录条数就有3亿1千多万,占用了50多G.那SQL SERVER 数据库中的表 ...

  4. hdu 2036:改革春风吹满地(叉积求凸多边形面积)

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. 关于使用AzureCli登陆提示SSLError的错误解决方案

    如果使用Powershell的azure cli命令登陆Azure时提示sslerror,大致如下的错误: 这个是由于你的网络前有网关造成的ssl验证没法通过. 解决方案: 在powershell中执 ...

  6. 《陪孩子像搭积木一样学编程》,一起来玩Scratch(1)使用Scratch编程的基本流程

    编程是一件很有趣的事情.初次接触编程,你可能不知所措,别担心,这并不复杂.首先,为了让读者对编程有大概的了解,可以把编写Scratch程序的过程分成7个步骤(如图1.8).注意,这是理想状态.在实际的 ...

  7. 初始化Weex项目遇到的问题记录

    Weex 提供了一个命令行工具 weex-toolkit 来帮助开发者使用 Weex.它可以用来快速创建一个空项目.初始化 iOS 和 Android 开发环境.调试.安装插件等操作. 目前 weex ...

  8. 运用fancybox弹出div的方式弹出视频界面

    fancybox可以弹出很多窗体,甚至一个swf格式的小视频.但这样的swf视频播放的时候并没有任何的控件.只能重头看到尾,或者关闭.我们可以利用fancybox弹出div盒子的方式配合html5很快 ...

  9. c++ 读写功能

    课程作业三 git链接: Operations 感想        这次代码修改的地方主要有,加入了文件读写.读出功能,以及分离函数写到了头文件里.        但是也有很多不足的地方,首先本来想要 ...

  10. JavaMail实现邮箱之间发送邮件功能

    package com.minstone.message.util; import java.util.Date; import java.util.Properties; import javax. ...