LeetCode 127. Word Ladder

Solution1:我的超过40%的AC的答案
原先利用BFS做但是内存溢出未能AC;进过修改加上了标记是否访问过的visited数组,可以AC啦~~~

class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {if (find(wordList.begin(), wordList.end(), endWord) == wordList.end())return 0;queue<string> str_queue;str_queue.push(beginWord);int level = 2, cur = 1, next = 0;vector<int> visited(wordList.size(), 0);while (cur) { string temp = str_queue.front();for (int i = 0; i < wordList.size(); i++) {if (! visited[i] && diff_n_char(temp, wordList[i]) == 1) {visited[i] = 1;if (wordList[i] == endWord)return level;str_queue.push(wordList[i]);next++;}}str_queue.pop();cur--;if (!cur) {cur = next;next = 0;level++;}                }return 0;}private:int diff_n_char(string& s1, string& s2) {int diff = 0;for (int i = 0; i < s1.size(); i++) {if (s1[i] != s2[i])diff++;}return diff;}
};

Solution2:
单向BFS,参考自花花酱:http://zxi.mytechroad.com/blog/searching/127-word-ladder/

class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> dict(wordList.begin(), wordList.end());        if (!dict.count(endWord)) return 0;queue<string> q;q.push(beginWord);int l = beginWord.length();int step = 0;while (!q.empty()) {++step;for (int size = q.size(); size > 0; size--) {                string w = q.front();                q.pop();for (int i = 0; i < l; i++) {                char ch = w[i];for (int j = 'a'; j <= 'z'; j++) {w[i] = j;// Found the solutionif (w == endWord) return step + 1;// Not in dict, skip itif (!dict.count(w)) continue;// Remove new word from dictdict.erase(w);// Add new word into queueq.push(w);                    }w[i] = ch;}}}return 0;}
};//仿写答案
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> word_set(wordList.begin(), wordList.end());if (!word_set.count(endWord)) //endWord不在wordList中return 0;queue<string> word_q;word_q.push(beginWord);int level = 1, len_word = beginWord.size();while (!word_q.empty()) {level++;for (int i = word_q.size(); i > 0; i--) {//倒着计数很聪明,简化了后面queue增、删元素引起的边界问题string w = word_q.front();word_q.pop();for (int j = 0; j < len_word; j++) {char temp_ch = w[j];for (int k = 'a'; k <= 'z'; k++) {w[j] = k;if (w == endWord) return level;if (!word_set.count(w)) continue;word_set.erase(w);word_q.push(w);}w[j] = temp_ch;}}}return 0;}
};

Solution3:
双向BFS,参考自花花酱:http://zxi.mytechroad.com/blog/searching/127-word-ladder/

class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> dict(wordList.begin(), wordList.end());        if (!dict.count(endWord)) return 0;int l = beginWord.length();unordered_set<string> q1{beginWord};unordered_set<string> q2{endWord};int step = 0;while (!q1.empty() && !q2.empty()) {++step;// Always expend the smaller queue firstif (q1.size() > q2.size())std::swap(q1, q2);unordered_set<string> q;for (string w : q1) {for (int i = 0; i < l; i++) {char ch = w[i];for (int j = 'a'; j <= 'z'; j++) {w[i] = j;if (q2.count(w)) return step + 1;if (!dict.count(w)) continue;                        dict.erase(w);q.insert(w);}w[i] = ch;}}std::swap(q, q1);}return 0;}
};

【重点BFS】LeetCode 127. Word Ladder相关推荐

  1. LeetCode 127. Word Ladder

    原题链接在这里:https://leetcode.com/problems/word-ladder/ 题目: Given two words (beginWord and endWord), and ...

  2. [LeetCode#127]Word Ladder

    ---恢复内容开始--- The problem: Given two words (start and end), and a dictionary, find the length of shor ...

  3. 【难点+重点BFS】LeetCode 126. Word Ladder II

    LeetCode 126. Word Ladder II Solution1: 参考自花花酱:http://zxi.mytechroad.com/blog/searching/leetcode-126 ...

  4. Leetcode的word ladder问题

    127. Word Ladder 题意如下: 给定一个开始字符串和结束字符串以及含有大量字符串的字典,要求通过一定规则,能从开始字符串变换到结束字符串,求其最小变换字符串数目,该规则如下: 1 从开始 ...

  5. 【LeetCode】127. Word Ladder 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...

  6. 127. Word Ladder

    文章目录 1 题目理解 2 BFS 3 双向BFS 1 题目理解 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度 ...

  7. 127. Word Ladder 单词接龙

    给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...

  8. 127.Word Ladder

    class Solution { public:int ladderLength(string start, string end, unordered_set<string> & ...

  9. 算法细节系列(20):Word Ladder系列

    算法细节系列(20):Word Ladder系列 详细代码可以fork下Github上leetcode项目,不定期更新. 题目摘自leetcode: 1. Leetcode 127: Word Lad ...

最新文章

  1. 车辆检测--DAVE: A Unified Framework for Fast Vehicle Detection and Annotation
  2. ASP.Net TextBox控件只允许输入数字
  3. JVM:内存划分总结
  4. 2682. 【WC2012选拔12.17】最长双回文串
  5. java html提取_2020年全新Java学习路线,含配套资料,更易上手 - 打不过就跑吧
  6. 搜索推荐炼丹笔记:Transformer在搜索推荐中的应用
  7. php tire树,Immutable.js源码之List 类型的详细解析(附示例)
  8. 锁底层之内存屏障与原语指令
  9. shell脚本实现菜单操作
  10. 对缓存的思考——提高命中率
  11. hdu 3926 hands in hands
  12. mysql数据库外键的作用
  13. 六款国产杀毒软件资源占用测试,八款杀毒软件横向评测:系统资源占用篇
  14. 锁屏中如何实现屏蔽home键
  15. 电脑怎么把mp4音频转换成mp3,电脑mp4转mp3的简单方法
  16. 共享单车信息系统服务器部署,共享单车云服务器搭建
  17. 觉得为时已晚的时候,恰恰是最早的时候。
  18. 计算网路地址,广播地址,第一位和最后一位可用地址,可用的IP地址
  19. linux下用命令修改图片像素
  20. struts1 使用poi组件 读取excel文件,创建excel ,输出excel文件

热门文章

  1. php几个问题的记录
  2. pandas 将DataFrame 转为txt文本,去除引号问题
  3. java自行车s码适合身高_捷安特s码适合多高
  4. java获取达梦数据库_记一次对达梦数据库的优化过程
  5. select 实现类似多线程_linux进程通信--socket套接字(四)--多路IO转实现一个server对应多个client...
  6. PHP函数中true表示什么,使用返回true或false的函数的PHP最佳实践是什么?
  7. mysql空指针异常处理_mysql 查询空指针异常
  8. c语言troubc int类型占几个字节,程序设计基础(C)第06讲例程
  9. 全国计算机等级考试报名入口黑龙江,黑龙江2021年3月计算机等级考试报名入口...
  10. 虚拟电脑键盘app_App发布倒计时