Word Ladder
2013-10-20 14:47
399 查看
I:
BFS
BFS
class Solution { public: int ladderLength(string start, string end, unordered_set<string> &dict) { // Note: The Solution object is instantiated only once and is reused by each test case. int res=1, n1=1, n2=0; queue<string> q; unordered_set<string> use; q.push(start); while (q.size()) { while (n1) { string ss=q.front(); q.pop(); n1--; for (int i=0; i<ss.size(); i++) { for (int j='a'; j<='z'; j++) { string s=ss; s[i]=(char) j; if (s==end) { return res+1; } if (dict.find(s)!=dict.end() && use.find(s)==use.end()) { q.push(s); n2++; use.insert(s); } } } } n1=n2; n2=0; res++; } return 0; } };
相关文章推荐
- Linux 使用故障小记
- 自动完成文本框的功能和用法
- HibernateDaoSupport和HibernateTemplate
- Android开发教程:shape和selector的结合使用
- 『HTML5梦幻之旅』-滚动播放的幻灯片效果
- hdu Portal(离线,并查集)
- 日期
- grunt Configuration
- Ei数据库文摘要求
- Effective C++读书笔记
- IDEA12 KeyGen Download List
- CSS属性
- 工作那些事(十二)如果哪一天,没有了电脑
- wordpress插件之baidu-sitemap-generator
- TCP/IP三次握手的过程和抓包分析原理
- 数字序列和为0--一串数字 添加符号 使其结果为0
- 安卓开发中selector的用法
- 堆排序
- 1223. Chernobyl’ Eagle on a Roof(dp)&&poj3783
- poj 3261 Milk Patterns(最长至少k次重复子串,后缀数组基础题)