LeetCode-Word Ladder
来源:互联网 发布:电脑软件图标大全 编辑:程序博客网 时间:2024/05/07 22:06
class Solution {public: int ladderLength(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function unordered_set<string> visited; queue<string> Q; visited.insert(start); Q.push(start); int dist = 1; int cnt = 1; while (cnt > 0) { while (cnt > 0) { string s = Q.front(); Q.pop(); for (int i = 0; i < s.size(); ++i) { char tmp = s[i]; for (char ch = 'a'; ch <= 'z'; ++ch) if (ch != tmp) { s[i] = ch; if (s == end) return dist + 1; if (dict.count(s) > 0 && visited.count(s) == 0) { visited.insert(s); Q.push(s); } } s[i] = tmp; } --cnt; } ++dist; cnt = Q.size(); } return 0; }};
参考:http://blog.sina.com.cn/s/blog_b9285de20101j1xl.html
- [Leetcode][python]Word Ladder/Word Ladder II
- [Leetcode] Word Ladder
- LeetCode: Word Ladder
- leetcode 84: Word Ladder
- [leetcode] word ladder
- 【leetcode】Word Ladder
- 【leetcode】Word Ladder II
- LeetCode - Word Ladder
- LeetCode之Word Ladder
- [LeetCode]Word Ladder
- [LeetCode]Word Ladder II
- [Leetcode]Word Ladder
- [leetcode]Word Ladder
- [leetcode] Word Ladder
- [leetcode] Word Ladder II
- LeetCode-Word Ladder
- LeetCode- Word Ladder
- LeetCode - Word Ladder II
- Android training(2)-Designing for Multiple Screens(为支持多种屏幕而设计)(A)
- FormatDateTime返回值为一种格式化后 字符串
- 数据结构作业
- 编程之美3.8 求二叉树中节点的最大距离
- poj 3233 Matrix Power Series 矩阵
- LeetCode-Word Ladder
- Oracle 表空间与数据文件
- 拓扑排序算法
- Java web开发中的编码问题
- 南阳-取石子
- Linux系统启动流程
- 我最爱的九妹和按钮事件
- 利用系统函数生成长整型的时间代码
- 手机耳机接口国家标准(OMTP)与国际标准(CTIA)区别