leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/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 intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
class node {
public:
string word;
int lv;
node(string s, int v): word(s), lv(v) {}
}; class Solution {
public: int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
int n = wordList.size(); if(!n) return ;
bool flag = false;
if(wordList.find(endWord) != wordList.end()) flag = true;
wordList.insert(endWord); queue<node> st;
st.push(node(beginWord, )); while(!st.empty()) {
node top = st.front();
st.pop();
int cur_lv = top.lv;
string cur_word = top.word; if(cur_word.compare(endWord) == ) return flag? cur_lv+: cur_lv;
unordered_set<string>::iterator p = wordList.begin(); for(int i=; i<cur_word.length(); ++i) {
for(char c = 'a'; c <= 'z'; ++c) {
char tmp = cur_word[i];
if(cur_word[i] != c) cur_word[i] = c;
if(wordList.find(cur_word) != wordList.end()) {
st.push(node(cur_word, cur_lv+));
wordList.erase(wordList.find(cur_word));
}
cur_word[i] = tmp;
}
}
} return ;
}
};
leetcode@ [127] Word Ladder (BFS / Graph)的更多相关文章
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
随机推荐
- uva 10739
dp 只有三个操作 当str[i] != str[j] 时 dp(i, j) = min(dp(i+1, j), dp(i+1, j-1), dp(i, j-1)) #include <ios ...
- ORACLE 更新关联多张表
UPDATE T_XMLC_BILL_HEAD_BAK T1 SET (T1.SENDRECEIVEFLAG, T1.SENDRECEIVEOPERATOR, T1.SENDRECEIVEDATE, ...
- Android中如何取消调转界面后EditText默认获取聚焦问题
最近在做一个项目,当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严重影响了用户体验.在网上找了资料,现总结如下. 我们知道,EditText有一个 android:foc ...
- win8 hyper-v 禁用不必卸载虚拟机
转载:http://tylzwp.blogbus.com/logs/232938121.html 禁用hyperv的目的是使用之前在用的VMware的虚拟机,不必重新处理一遍. 具体操作: 1确报之前 ...
- linux进程模型总结
Linux进程通过一个task_struct结构体描述,在linux/sched.h中定义,通过理解该结构,可更清楚的理解linux进程模型. 包含进程所有信息的task_struct数据 ...
- 初始化一台linux server来做项目管理和测试
毕业以后很多没做过这么技术的事情了,不过今年要开始咯. Goal: 练手安装Nginx,并且配置不同的server,后端有Tomcat的(JIRA),有PHP(总得有的),还有Tornado和Node ...
- 安装universal-ctags
universal-ctags是exuberant-ctags的替代者,相比gtags可以解析更多的语言,但是其他方面比如操作,数据库的组织方式等就不够好.需要自己编译安装 先用 git clone ...
- JS判断是否出现滚动条
http://www.cnblogs.com/yazdao/archive/2010/12/06/1897742.html 该博文是想用JS检测浏览器是否出滚动条. 这边想到一个比较取巧的方法, 假如 ...
- Android开发之ContentValues
SQLite数据库进行CRUD的时候, 用到了ContentValues类,负责存储名值对,名都是String类型,值都是基本类型. 例子: ContentValues values=new Cont ...
- centos 如何用 rsyslog 搭建本地日志服务
一.问题背景 最近项目遇到一个问题,服务器响应很慢,team中的两个有经验的工程师找了一台服务器分析了一下,发现问题出在磁盘写入过于频繁.这里大概介绍一下背景,我们的服务器上面主要是跑各种PHP接口, ...