leetcode — Word Ladder

leetcode — Word Ladder

爱情也有好赖,决不能草率,我是愿意等待,哪怕青春不再

 [问题描述]

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["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.

[解题思路]

BFS经典场景

 1 int Solution::ladderLength(std::string start, std::string end, std::tr1::unordered_set<std::string> &dict)
 2 {
 3     std::queue<std::pair<std::string, int> > bfs;
 4     std::tr1::unordered_set<std::string> path;
 5     path.insert(start);
 6     bfs.push(std::make_pair(start, 2));
 7     while(!bfs.empty()){
 8         std::string tmp = bfs.front().first;
 9         int num = bfs.front().second;
10         for (int i = 0; i < tmp.length(); i ++){
11             tmp = bfs.front().first;
12             for(char ti = 'a'; ti <= 'z'; ti ++){
13                 tmp[i] = ti;
14                 if (tmp == end)
15                     return num;
16                 if (dict.find(tmp) != dict.end() && path.find(tmp) == path.end()){
17                     bfs.push(std::make_pair(tmp, num + 1));
18                     path.insert(tmp);
19                 }
20             }
21         }
22         bfs.pop();
23     }
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/taizy/p/3910818.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109754.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • springmvc 适配器详解[通俗易懂]

    springmvc 适配器详解[通俗易懂]大家知道springmvc是一个非常优良的框架,配置灵活实现简单,只需我们更多的关注我们的业务逻辑。今天我们就通过一个简单的例子模拟适配生成过程。处理器适配器HandlerAdapter1、SimpleControllerHandlerAdapter表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean可以作

    2022年5月12日
    40
  • gtest整理_softest

    gtest整理_softest目录简介使用目的使用时机使用方法测试样例使用心得简介gtest是一个跨平台的C++单元测试框架,由google公司发布。gtest提供了丰富的断言,供开发者对代码块进行白盒测试。使用目的测试代码逻辑是否正确。编译器只能检测出语法错误但是无法检测到逻辑错误,比如一个函数或类是否完成了期望的功能。单元测试可以帮助我们判断代码设计得是否清晰合理。一块代码的逻辑越清晰,它的单元测试就可以设计得越简单。方便并行开发。一个程序的有不同模块相互耦合,某个模块未完成可能影响其他已完成模块的测试,这时可以利用

    2022年9月1日
    4
  • springboot 406错误 produces(springboot下载)

    Springboot406错误简单的写了一个接口,然后406了。。。。publicclassTestController{@GetMapping(“/testGet/{message}”)publicResulttestGet(@PathVariableStringmessage){returnResult.success(message);}}Resolved[org.springframework.web.HttpMedi

    2022年4月12日
    47
  • Linux stat函数_linux C编程视频

    Linux stat函数_linux C编程视频linuxC函数之stat函数1.函数功能:通过文件名filename获取文件信息,并保存在buf所指的结构体stat中2.函数原型1)函数头文件#include#include2)函数intstat(constchar*file_name,structstat*buf)3)返回返回值:     执行成功则返回0,失败返回-1,错误代码存于errno错误代码:ENOENT  

    2022年8月21日
    11
  • java sql拼接字符串_SQL中字符串拼接

    java sql拼接字符串_SQL中字符串拼接1.概述在SQL语句中经常需要进行字符串拼接,以sqlserver,oracle,mysql三种数据库为例,因为这三种数据库具有代表性。sqlserver:select’123’+’456′;oracle:select’123’||’456’fromdual;或selectconcat(‘123′,’456’)fromdual;mysql:selectconcat(‘123’,’…

    2022年6月16日
    102

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号