LeetCode: Word Ladder
直接看答案。。
1 class Solution { 2 public: 3 int ladderLength(string start, string end, unordered_set<string> &dict) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if (start.size() != end.size() || !dict.size()) return 0; 7 int dis = 1; 8 queue<string> queTopush, queTopop; 9 queTopop.push(start); 10 while (dict.size() && !queTopop.empty()) { 11 while (!queTopop.empty()) { 12 string str = queTopop.front(); 13 queTopop.pop(); 14 for (int i = 0; i < str.size(); i++) { 15 for (char j = 'a'; j <= 'z'; j++) { 16 if (j == str[i]) continue; 17 char tmp = str[i]; 18 str[i] = j; 19 if (str == end) return dis+1; 20 if (dict.count(str)) { 21 queTopush.push(str); 22 dict.erase(str); 23 } 24 str[i] = tmp; 25 } 26 } 27 } 28 queTopop.swap(queTopush); 29 dis++; 30 } 31 return 0; 32 } 33 };
转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/04/23/3036937.html
LeetCode: Word Ladder相关推荐
- [leetcode]Word Ladder
此题要多品品.由于是求最短的路径,所以用BFS,DFS会在大数据的时候超时,原因也容易理解.最终解法参考了:http://discuss.leetcode.com/questions/1108/wor ...
- 【难点+重点BFS】LeetCode 126. Word Ladder II
LeetCode 126. Word Ladder II Solution1: 参考自花花酱:http://zxi.mytechroad.com/blog/searching/leetcode-126 ...
- 【重点BFS】LeetCode 127. Word Ladder
LeetCode 127. Word Ladder Solution1:我的超过40%的AC的答案 原先利用BFS做但是内存溢出未能AC:进过修改加上了标记是否访问过的visited数组,可以AC啦~ ...
- LeetCode 127. Word Ladder
原题链接在这里:https://leetcode.com/problems/word-ladder/ 题目: Given two words (beginWord and endWord), and ...
- Leetcode的word ladder问题
127. Word Ladder 题意如下: 给定一个开始字符串和结束字符串以及含有大量字符串的字典,要求通过一定规则,能从开始字符串变换到结束字符串,求其最小变换字符串数目,该规则如下: 1 从开始 ...
- PHP第五周答案,算法设计与分析第五周作业——Word Ladder
算法设计与分析第五周作业--Word Ladder 上周找了一道深度搜索优先搜索的算法题来做,于是这周就选了一道广度优先搜索算法题来试试手. 本周所选题目:原题目链接 题目详情 题目大意:给出一个字符 ...
- 算法细节系列(20):Word Ladder系列
算法细节系列(20):Word Ladder系列 详细代码可以fork下Github上leetcode项目,不定期更新. 题目摘自leetcode: 1. Leetcode 127: Word Lad ...
- Word Ladder
LeetCode[127. Word Ladder] 题解 难度[medium] 题目: Given two words (beginWord and endWord), and a dictiona ...
- LeetCode——Word Break
LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...
最新文章
- Java程序员修炼之路(一)我们为什么选择Java
- python计算生态的命名_Python计算生态之random库
- NYOJ 711 最舒适的路线(并查集)
- Java EE EJB拦截器教程和示例
- mybatis collection_MyBatis之关联查询
- Jfinal集成Spring
- 机器学习Scikit-Learn基本操作实战
- java面试题 springmvc 解决post get请求乱码问题
- 使用commons-compress操作zip文件(压缩和解压缩)
- 2022软件测试技能 Jmeter+Ant+Jenkins持续集成并生成测试报告教程
- Reg命令使用详解 批处理操作注册表必备
- 冬暖夏凉究竟香不香?带恒温的TaoTronics暖风机开箱实测
- VR/AR眼镜Type-C接口边用边PD快充方案
- 马后炮之12306抢票工具(二) -- 联系人获取车次
- 【作业】非结构化数据相关知识整理
- TI芯片(335x、437x、572x)uboot设置logo居中
- conda search cuda后没有版本10的问题
- 查看当前目录和上一级目录的命令
- 割完肉先疗伤(转自:沙牛家书)
- 计算机课怎么给老师发消息,案例 | 信息化课堂 老师如何教
热门文章
- 程序员都在用的IDEA插件(不断更新)
- [gtest][002] A quick start to build the Google C++ Testing project
- jwt重放攻击_JWT+ASP.NET MVC 时间戳防止重放攻击
- oracle u01目录 100,文件目录空间利用率达到100%而导致数据库异常挂起的故障处理过...
- linux 命令查看块设备,系统运维|用 Linux blkid 命令查找块设备详情
- itext 添加空格_itext7史上最全实战总结
- docker启动mysql容器后又退出_docker容器刚运行就自动退出了
- inner join 重复数据_Ramp;Python Data Science 系列:数据处理(2)
- ssh不能进入save方法_快速理清为什么你的redis未授权不能getshell
- 如何把hello.c 输出到txt_如何在程序开发中进行文件的相关操作呢?往这看