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. linux pwd命令查看当前路径命令

    命令简介: 该命令用来显示目前所在的工作目录.指令英文原义:print work directory执行权限 :All User指令所在路径:/usr/bin/pwd 或 /bin/pwd 命令语法: ...

  2. Axis开发Web Service

    可以自动生成代码的 一.Axis环境的安装 1.安装环境 J2SE SDK 1.4,Tomcat 5.0,eclipse 3.2. 2.到 http://xml.apache.org 网站下载Axis ...

  3. 统计中的bitMap

    //位图的概念就是在个一字节八位的地方存八个状态 比如 bool hash[] 表示某个数字被标记过,一个数字需要一个字节 而bitMap就是可以把每位都用来标记,起到节约空间的目的 //位图的概念就 ...

  4. 详解Centos7 修改mysql指定用户的密码

    本文介绍了Centos7 修改mysql指定用户的密码,具体如下: 1.登陆mysql或者mariadb(两种任选其一) [root@localhost ~]# mysql -u root [root ...

  5. 【详解】Linux的文件描述符fd与文件指针FILE*互相转换

    使用系统调用的时候用文件描述符(file descriptor,简称fd)的时候比较多,但是操作比较原始.C库函数在I/O上提供了一些方便的包装(比如格式化I/O.重定向),但是对细节的控制不够. 如 ...

  6. 【ZZ】国外大型网站使用到编程语言 | 菜鸟教程

    http://www.runoob.com/w3cnote/rogramming-languages-used-in-most-popular-websites.html 下图展示了大型网站使用到的后 ...

  7. 思科、华为、H3C命令对照表

    思科 华为 H3C 描述 no undo undo 取消/关闭 当前设置 show display display 查看.显示 exit quit quit 退回上级 hostname sysname ...

  8. hive 创建/删除/截断 表(翻译自Hive wiki)

    这里罗列常用操作,更多参考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL- ...

  9. Pycharm上python和unittest两种姿势傻傻分不清楚

    前言 经常有人在群里反馈,明明代码一样的啊,为什么别人的能出报告,我的出不了报告:为什么别人运行结果跟我的不一样啊... 这种问题先检查代码,确定是一样的,那就是运行姿势不对了,一旦导入unittes ...

  10. ubantu的下载和在虚拟机VM中的安装

    一.下载Ubuntu镜像文件 下载地址:http://www.ubuntu.com 打开上面的连接,我们来到如下页面,点击download: 下一步,到如下页面,点击Ubuntu Desktop: 然 ...