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:

  1. Only one letter can be changed at a time
  2. 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.

思路:

Step1:把本题看成是树状结构

Step2:通过两个队列,实现层次搜索

class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
queue<string> queue_to_push;
queue<string> queue_to_pop;
bool endFlag = false;
string curStr;
int level = ;
char tmp; queue_to_pop.push(beginWord);
wordList.erase(beginWord); //if beginWord is in the dict, it should be erased to ensure shortest path
while(!queue_to_pop.empty()){
curStr = queue_to_pop.front();
queue_to_pop.pop(); //find one letter transformation
for(int i = ; i < curStr.length(); i++){ //traverse each letter in the word
for(int j = 'a'; j <= 'z'; j++){ //traverse letters to replace
if(curStr[i]==j) continue; //ignore itself
tmp = curStr[i];
curStr[i]=j;
if(curStr == endWord){
return level+;
}
else if(wordList.count(curStr)>){ //occur in the dict
queue_to_push.push(curStr);
wordList.erase(curStr);
}
curStr[i] = tmp; //back track
}
} if(queue_to_pop.empty()){//move to next level
swap(queue_to_pop, queue_to_push);
level++;
}
}
return ;
}
};

127. Word Ladder (Tree, Queue; WFS)的更多相关文章

  1. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  4. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  5. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  6. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  7. 126. Word Ladder II( Queue; BFS)

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  9. 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

随机推荐

  1. python实现八大排序算法

    插入排序 核心思想 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为 O(n^2).是稳定的排序方法.插入算法 ...

  2. git 解决冲突问题

    问题描述: 一般来说,只要多人提交同一仓库,必然会有冲突的发生. Git解决 利用git解决冲突很简单,最常规的方法莫过于工作之前先更新整个project,完成之后及时提交.然而尽管这样做,也难免有人 ...

  3. 20155315 2016-2017-2 《Java程序设计》第八周学习总结

    教材学习内容总结 第14章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区(Buffer)容量,在缓冲区中对感兴趣的数据区块进 ...

  4. 注册dll文件

    1.打开"开始-运行-输入regsvr32 XXX.dll",回车即可 2.win7 64位旗舰版系统运行regsvr32.exe提示版本不兼容 在运行regsvr32.exe的时 ...

  5. (精华)将json数组和对象转换成List和Map(小龙哥和牛徳鹤的对话)

    将java标准的数据结构ArrayList和HashMap转换成json对象和数组很简单 只需要JSONArray.fromObject(obj);或者JSONObject.fromObject(ob ...

  6. Linux下编写动态链接库

    下面通过一个例子来介绍如何生成一个动态库.这里有一个头文件:so_test.h,三个.c文件:test_a.c.test_b.c.test_c.c,我们将这几个文件编译成一个动态库:libtest.s ...

  7. UT报错误:A granted authority textual representation is required

    原因:团队唯一标识数据为空,必须保证唯一 牵连需要改进的代码: UserDetailService.java 60行"初始化角色集合"未进行异常处理

  8. Webpack-simple cross-env 不是内部或外部命令问题处理

    本文转载自:https://www.cnblogs.com/stono/p/6984222.html Webpack-simple cross-env 不是内部或外部命令问题处理 学习了:https: ...

  9. windows mysql默认配置文件

    查询配置目录 select @@basedir; 查询数据目录 select @@datadir; 查询数据库编码 show variables like 'char% my.ini [mysql] ...

  10. Spring MVC @ResponseBody和@RequestBody使用

    @ResponseBody用法: 作用:该注解用于将Controller的方法返回的对象,根据HTTP Request Header的Accept的内容,通过适当的HttpMessageConvert ...