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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 深入理解双亲委派机制及作用「建议收藏」

    java双亲委派机制及作用一、什么是双亲委派机制当某个类加载器需要加载某个.class文件时,它首先把这个任务委托给他的上级类加载器,递归这个操作,如果上级的类加载器没有加载,自己才会去加载这个类。二、类加载器BootstrapClassLoader(启动类加载器)c++…

    2022年4月17日
    76
  • 【C#基础】-Substring截取字符串的方法小结

    【C#基础】-Substring截取字符串的方法小结前言    在公司的图书馆项目中曾经用过截取字符串的方法,项目是java语言的;最近在公司的另一个项目中又需要截取字符串,一种环境是C#语言,一种环境是SQLServer存储过程;先来说一下后台程序中截取字符串的方法。正文c#中截取字符串主要是借助Substring这个函数。stringstring.Substring(intstartIndex,intlength)

    2022年5月10日
    36
  • mybatis自定义分页_java分页查询接口的实现

    mybatis自定义分页_java分页查询接口的实现问题出现原因是集成mybaits时会自动加上selecttmp_page.*,rownumrow_idfrom(查询语句)tmp_page出现这个问题的原因是查询语句的列有重复的,直接查询是看不出来原因的,把重复的列名找出来然后修改…

    2022年10月4日
    4
  • 三层架构(我了解并详细分析)

    三层架构(我了解并详细分析)

    2021年12月31日
    49
  • jmeter的正则表达式提取器_jmeter正则提取器的使用

    jmeter的正则表达式提取器_jmeter正则提取器的使用当我们的请求有这种类型的多种数据,我们要怎么获取到全部?首先,先在正则表示式提取器里面添加我们找到的左右边界然后写好正则表达式最后确定边界是唯一的然后我们运行一下,一下只就运行全部出来了…

    2025年8月26日
    9
  • redis 配置密码验证_spring redis配置

    redis 配置密码验证_spring redis配置redis配置密码1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到#requirepassfoobared去掉行前的注释,并修改密码为所需的密码,保存文件requirepassmyRedis重启redissudoserviceredisrestart#或者sudoservicerediss

    2025年9月3日
    8

发表回复

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

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