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 ...
随机推荐
- firefly的环境搭建(2013年9月25日最新,win下最详图文)
源地址:http://www.9miao.com/question-15-53785.html 一.安装PythonFirefly是采用Python编写的高性能.分布式游戏服务器框架,所以使用Fire ...
- 服务器部署_linux下部署jprofiler简单备忘
1.windows安装jprofiler 2.linux下安装jprofiler服务端,记好安装路径.假设是安装在/ex/bin/下 3. 配置tomcat的启动sh文件,在后面加入以下参数: -a ...
- 【疯狂Java学习笔记】【第一章:Java语言概述】
[学习笔记]1.Java与C#的异同:Java与C#的相同之处有很多,它们均摒弃了C++面向对象的多继承.宏定义.全局变量.全局函数.指针等等难以使用的机制,添加进了成熟的机制,如垃圾回收.接口等等. ...
- Linux内核中的中断
http://blog.csdn.net/weiqing1981127/article/details/8298585 中断处理程序是被内核调用来响应中断的,它运行在中断上下文,中断处理程序是上半部, ...
- eclipse为hibernate.cfg.xml添加自动提示【转】
在hibernate.cfg.xml头部部分如下: <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate C ...
- Android开发之注解式框架ButterKnife的使用
ButterKnife官网 其实ButterKnife的注解式,与xUtils的ViewUtils模块基本上差不多,只要用过xUtils,这个框架基本上就会了. 一.原理. 最近发现一个很好用的开源框 ...
- poj3034Whac-a-Mole(dp)
链接 状态转移好想 不过有坑 大家都犯的错误 我也会犯 很正常 就是锤子可以移到n*n以外 要命的是我只加了5 以为最多不会超过5 WA了N久 才想到 上下两方向都可以到5 所以最多加10 以时 ...
- Can't obtain the input stream from /docProps/app.xml
今天在做poi修改样式时,报了以下错误: Exception in thread "main" org.apache.poi.POIXMLException: java.io.IO ...
- Jquery动画第二部分
效果图: →→→→→→ <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...
- codevs3731 寻找道路
方向dfs判定是否可行,spfa跑最短路. noip水题,wa好几次. #include<cstdio> #include<algorithm> #include<cst ...