Word Ladder题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/word-ladder/description/


Description

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 transformed word must exist in the word list. Note that beginWord is not a transformed word.

For example,

Given:

beginWord = "hit"

endWord = "cog"

wordList = ["hot","dot","dog","lot","log","cog"]

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.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

UPDATE (2017/1/20):

The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

Solution

class Solution {
private:
set<string> isVisited;
map<string, string> preVertex;
public:
bool canTrans(string a, string b) {
int count = 0;
for (int i = 0; i < a.length(); i++)
if (a[i] != b[i]) {
if (count == 0)
count++;
else
return false;
}
return count == 1;
} int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
if (wordList.size() == 0)
return 0;
isVisited.insert(beginWord);
queue<string> vq;
vq.push(beginWord);
string qfront;
bool finish = false;
while (!finish && !vq.empty()) {
qfront = vq.front();
vq.pop();
for (auto& w: wordList) {
if (canTrans(qfront, w) && isVisited.count(w) == 0) {
isVisited.insert(w);
vq.push(w);
preVertex[w] = qfront;
if (w == endWord) {
finish = true;
break;
}
}
}
}
if (finish) {
int len = 1;
string v = endWord;
while (v != beginWord) {
v = preVertex[v];
len++;
}
return len;
} else {
return 0;
}
}
};

解题描述

这道题我采用的是BFS去查找指定的终点。同时用一个map来记录每一个已经被搜索的顶点的前一个点。

[Leetcode Week5]Word Ladder的更多相关文章

  1. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  2. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. [LeetCode] 126. Word Ladder II 词语阶梯 II

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

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

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

  5. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  6. [LeetCode] 126. Word Ladder II 词语阶梯之二

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

  7. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  8. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  9. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. Selenium八大元素定位方式

    1.根据id来定位: import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.sele ...

  2. 常用模块(数据序列化 json、pickle、shelve)

    本节内容 前言 json模块 pickle模块 shelve模块 总结 一.前言 1. 现实需求 每种编程语言都有各自的数据类型,其中面向对象的编程语言还允许开发者自定义数据类型(如:自定义类),Py ...

  3. 详谈P(查准率),R(查全率),F1值

    怎么来的? 我们平时用的精度accuracy,也就是整体的正确率 acc = predict_right_num / predict_num 这个虽然常用,但不能满足所有任务的需求.比如,因为香蕉太多 ...

  4. Java生成C#可用Model包

    项目需要提供接口给.NET团队使用,为方便大伙,特地写一个从Java接口生成C#可用Model包的工具Class 主Class是一个Controller,可以随时进行生成 package com.fa ...

  5. pta指针作业

    #PTA实验作业 6-1 本题pta提交列表 设计思路 本题是一道简单的指针程序题,两个数已经分别被指针定义,只要把用其指针把二者加在一起和减去即可 调试过程 本题无调试过程 代码截图 6-2  1. ...

  6. C - 安装雷达

    C - 安装雷达 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Descr ...

  7. 201621044079 week07-JAVA GUI类

    作业07-Java GUI编程 1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模 ...

  8. Graphic的一些基本概念

    做了张很丑陋的图,估计还不准确...先凑合看吧~

  9. Struts1之html标签

    Struts1的html标签主要是为了绘制HTML页面标签元素,通过与Struts1框架的集成,增强功能 首先,在使用前,需要引入tld文件 <%@ taglib prefix="ht ...

  10. tomcat 路径"/"表示根目录