首页 技术 正文
技术 2022年11月10日
0 收藏 402 点赞 4,379 浏览 6030 个字

题目描述

给定两个单词(初始单词和目标单词)和一个单词字典,请找出所有的从初始单词到目标单词的最短转换序列:每一次转换只能改变一个单词每一个中间词都必须存在单词字典当中例如:给定的初始单词start=”hit”,目标单词end =”cog”。单词字典dict =[“hot”,”dot”,”dog”,”lot”,”log”]返回的结果为:

  [↵    ["hit","hot","dot","dog","cog"],↵    ["hit","hot","lot","log","cog"]↵  ]

注意:

题目中给出的所有单词的长度都是相同的 题目中给出的所有单词都仅包含小写字母

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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”]

Return

  [↵    ["hit","hot","dot","dog","cog"],↵    ["hit","hot","lot","log","cog"]↵  ]↵

class Solution {public:/*    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {        vector<vector<string>> paths;        vector<string> path(1, start);        if(start == end){            paths.push_back(path);            return paths;        }        unordered_set<string> forward, backward;        forward.insert(start);        backward.insert(end);        unordered_map<string, vector<string>> nexts;        bool isForward = false;        if(findLaddersHelper(forward, backward, dict, nexts, isForward))            getPath(start, end, nexts, path, paths);        return paths;    }private:    bool findLaddersHelper(unordered_set<string> &forward,                           unordered_set<string> &backward,                           unordered_set<string> &dict,                           unordered_map<string, vector<string>> nexts,                           bool &isForward){        if(forward.empty())            return false;        if(forward.size() > backward.size())            return findLaddersHelper(backward, forward, dict, nexts, isForward); //从words数较少的一边开始寻路        for(auto it=forward.begin(); it!=forward.end(); it++)            dict.erase(*it);        for(auto it=backward.begin(); it!=backward.end(); it++)            dict.erase(*it);        unordered_set<string> nextLevel;        bool reach = false;        for(auto it=forward.begin(); it!=forward.end(); ++it){            string word = *it;            for(auto ch=word.begin(); ch!=word.end(); ++ch){                char tmp = *ch;                for(*ch='a'; *ch<='z'; ++(*ch)){                    if(*ch != tmp) //遍历除自身外的25个字母                        if(backward.find(word) != backward.end()){                            reach = true; //走到了末尾                            isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it);                        }                        else if(!reach && dict.find(word) != dict.end()){                            nextLevel.insert(word);                            isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it);                        }                }            *ch = tmp;            }        }        return reach || findLaddersHelper(backward, nextLevel, dict, nexts, isForward);    }         void getPath(string beginWord, string &endWord,                 unordered_map<string, vector<string>> &nexts,                  vector<string> &path, vector<vector<string>> &paths){        if(beginWord == endWord)            paths.push_back(path);        else            for(auto it=nexts[beginWord].begin(); it!=nexts[beginWord].end(); ++it){                path.push_back(*it);                getPath(*it, endWord, nexts, path, paths);                path.pop_back();            }    }*/     vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {        vector<vector<string> > paths;        vector<string> path(1, start);        if (start == end) {//首位words相同            paths.push_back(path);            return paths;        }        unordered_set<string> forward, backward;        forward.insert(start);        backward.insert(end);        unordered_map<string, vector<string> > nexts; //存储路径的矩阵        bool isForward = false;        if (findLaddersHelper(forward, backward, dict, nexts, isForward))            getPath(start, end, nexts, path, paths);        return paths;    }private:    bool findLaddersHelper(        unordered_set<string> &forward,        unordered_set<string> &backward,        unordered_set<string> &dict,        unordered_map<string, vector<string> > &nexts,        bool &isForward) {        isForward = !isForward; //反转方向标志??        if (forward.empty())            return false;        if (forward.size() > backward.size())            return findLaddersHelper(backward, forward, dict, nexts, isForward);//从words数较少的一边开始寻路        for (auto it = forward.begin(); it != forward.end(); ++it) //已放入前向 后向数组中的words从dict去除            dict.erase(*it);        for (auto it = backward.begin(); it != backward.end(); ++it)            dict.erase(*it);        unordered_set<string> nextLevel;        bool reach = false; //寻路未完成        for (auto it = forward.begin(); it != forward.end(); ++it) {//广度遍历前向数组中的每一个分支            string word = *it;            for (auto ch = word.begin(); ch != word.end(); ++ch) {                char tmp = *ch;                for (*ch = 'a'; *ch <= 'z'; ++(*ch))//遍历除自身外的25个字母                    if (*ch != tmp)                        if (backward.find(word) != backward.end()) { //前后向数组成功相接                            reach = true; //寻路完成                            isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it);                        }                        else if (!reach && dict.find(word) != dict.end()) { //未到达 且 字典中有需要的words                            nextLevel.insert(word); //将新产生的分支放入临时数组,用于下次递归调用                            isForward ? nexts[*it].push_back(word) : nexts[word].push_back(*it);                        }                        *ch = tmp;            }        }        return reach || findLaddersHelper(backward, nextLevel, dict, nexts, isForward);    }    void getPath(        string beginWord,        string &endWord,        unordered_map<string, vector<string> > &nexts,        vector<string> &path,        vector<vector<string> > &paths) {        if (beginWord == endWord) //走到了,将path中的值压入paths            paths.push_back(path);        else            for (auto it = nexts[beginWord].begin(); it != nexts[beginWord].end(); ++it) {                path.push_back(*it);                getPath(*it, endWord, nexts, path, paths);                path.pop_back(); //每退出一次递归,将该层压入的值弹出            }    }};



相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295