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. cn_office_Professional_Plus_2010_vol_with_sp2-【x86+x64仅2.45GB】

    用官方sp2补丁在cn_office_Professional_Plus_2010_vol基础上集成,无其他任何添加及修改! 文件: cn_office_Professional_Plus_2010_ ...

  2. js网页 唤醒支付宝

    过渡页: <script> window.location.href = 'alipays://platformapi/startApp?appId=10000011&url=al ...

  3. 堆排序(C语言实现)

    一.堆的概念 所谓堆,它是一个数组,也能够被看成一个近似的全然二叉树.树上每一个结点相应数组的一个元素.二叉堆分为二种:最大堆和最小堆.本文主要介绍最大堆,最小堆类似.最大堆的特点:对于随意某个结点, ...

  4. 获取bing带swim的网址列表

    需求背景: 应老婆要求,搜集带有swim关键字的网站.实现过程: 使用requests模块通过bing接口搜索swim关键,将返回内容按需求进行处理,得到网站列表. 注:代码比较拙,老司机就不要弄废时 ...

  5. Python DB

    #!/usr/bin/python #_*_ coding:utf-8 _*_ import MySQLdb import time import threading import random fr ...

  6. 【free() invalid next size】谨慎地在C++的类中存储指针来方便访问其他节点

    “我跟你们说,你们知道STL容器,vector/string/deque等等,都有个reserve方法吗?你们一个个地push_back,嫌C++比C慢,怪谁?” “要像我这样,预先分配足够大的空间, ...

  7. 黄聪:wordpress向mysql字段中保存html代码(使用add_option()方法),然后无法显示出问题

    你可以把" 引号去掉了再进库,或者使用 stripslashes_deep() <?php $str = "Is your name O\'reilly?"; // ...

  8. lodop打印控件需要开启的几个计算机服务

    首先要开启: 其次:

  9. Java 获取字符串长度 length()

    Java 手册 实例: public class Length { public static void main(String[] args) { String str = "hgdfas ...

  10. Java-Runoob-高级教程-实例-环境设置实例:1.Java 实例 – 如何编译一个Java 文件?

    ylbtech-Java-Runoob-高级教程-实例-环境设置实例:1.Java 实例 – 如何编译一个Java 文件? 1.返回顶部 1. Java 实例 - 如何编译 Java 文件  Java ...