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


相关推荐

  • vc 获取月份的天数_根据距离1900年1月1日的天数(0起始),计算这一天的日期(年,月,日)…

    vc 获取月份的天数_根据距离1900年1月1日的天数(0起始),计算这一天的日期(年,月,日)…我的代码,算法不优美,但结果正确^_^#include#include#includevoidfoo(int&year,int&month,int&day){year=1900;month=1;day+=0;for(intn;n=(((year%4==0&&year%100!=0)||(year%400==0))…

    2022年8月12日
  • 推荐几个bootstrap 后端UI框架

    推荐几个bootstrap 后端UI框架工欲善其事,必先利其器对于从事软件开发的您也一样,有一套熟悉的bootstrap后台ui框架,bootstrap后端模板让您的开发速度大幅度提升这是本人经常使用到的一些bootstrap后台框架推荐给大家第一名inspiniabootstrap后端模板演示地址http://cn.inspinia.cn效果图cn.inspinia.cn第二名nifty…

    2022年6月10日
  • Android源码在线查看网站

    Android源码在线查看网站安卓社区https://www.androidos.net.cn/sourcecode(推荐)AndroidXRefhttp://androidxref.com/安卓特性(包括历史版本):https://developer.android.google.cn/

    2022年7月20日
  • JAVA增强for循环

    JAVA增强for循环增强for循环为什么需要增强for循环呢?简化数组和集合的遍历。想要使用增强for循环必须先要了解它的格式增强for循环的格式for(数据类型变量名:数组或者集合对象){ //循环体,变量即元素}下边我们通过案例演示一下://需求通过增强for遍历list集合。publicstaticvoidmain(String[]args){ //复习一下如何创建List集合 //1.创建元素对象 Listlist=newArrayList(); }…

    2022年6月16日
  • 8. 强化学习之——模仿学习

    8. 强化学习之——模仿学习课程大纲注:本文所有内容源自于B站周博磊老师更新完的强化学习纲要课程,听完之后获益很多,本文也是分享我的听课笔记。周老师Bilibili视频个人主页:https://space.bilibili.com/511221970?spm_id_from=333.788.b_765f7570696e666f.2感谢周老师:)…

    2022年9月18日
  • android+号码归属地数据库,Android手机号码归属地的查询「建议收藏」

    android+号码归属地数据库,Android手机号码归属地的查询「建议收藏」一个简单的Demo,从聚合数据申请手机号码归属地数据接口;在EditText中输入待查询号码,获取号码后在子线程中使用HttpUrlconnection获取JSON数据,之后进行解析;数据获取完成后,在主线程中更新UI,显示获取的号码归属地信息。布局文件android:layout_width=”match_parent”android:layout_height=”match_parent”an…

    2022年7月22日

发表回复

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

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