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 ...
随机推荐
- unity3d与eclipse集成开发android应用
原地址:http://blog.csdn.net/armoonwei/article/details/7032537 Unity as a Library Once you have eclipse ...
- python参考手册--第2章词汇和语法约定
1.续行符\ 三引号.().{}.[]中的内容不需要续行符 2.空格缩进 优选空格作为缩进,不要用tab,这是因为不同操作系统下tab对应的空格不一样,而python是通过严格的空格来控制语句块的. ...
- Pie Charts
Default pie chart The default pie chart with no options set. Source Code $.plot('#placeholder', da ...
- 1030-ACM程序设计之马拉松竞赛
描述 校ACM协会近四个月举行了为期100天ACM程序设计之马拉松竞赛,竞赛题总数为1000,同学们反响热烈,先后有许多ACM程序设计竞赛爱好者开始先后编号,成功解答的题目数为选手的成绩. 今天进行成 ...
- POJ3096Surprising Strings(map)
题意:输入很多字符串,以星号结束.判断每个字符串是不是“Surprising Strings”,判断方法是:以“ZGBG”为例,“0-pairs”是ZG,GB,BG,这三个子串不相同,所以是“0-un ...
- PHP开篇之环境的搭建
PHP开篇之环境的搭建 Wamp软件下载:http://www.wampserver.com/ 此时是2.5版本 下载下来一键安装. 安装有个主意 这里先不用管 或者smtp@qq.com 13643 ...
- 万网空间如何安装wordpress
万网空间如何安装wordpress建站教程 _ 学做网站论坛 http://www.xuewangzhan.com/wpbbs/1643.html 1.先在本地下载一个最新版本的wordpress ...
- 好用的linux命令
sudo chown -R `whoami` /usr/local # ps aux |grep php-fpm php-frm start and stop php-fpm -D killall p ...
- Android 九宫格密码锁进入程序
设置九宫格密码锁进入程序,设置,重置,取消等,安卓巴士地址http://www.apkbus.com/forum.php?mod=viewthread&tid=182620&extra ...
- Android开发经验记录
一. 代码规范 定一个规范的主要目的,是为了让不同的开发人员写的代码能保持一致性,方便别人看自己的代码.另外,对个人来说,也能起到让自己看着舒服的作用. 1. 基本 * 使用UTF-8 ...