当前位置: 首页 > news >正文

水果网站怎么做引擎优化是什么意思

水果网站怎么做,引擎优化是什么意思,网站系统参数设置,网站网站制作公司哪家好文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析:示例1为例,hit到达cog的路线不止一条,如何找到最短是关键。广度优先搜索是一圈…

文章目录

  • 一、题目
  • 二、解法
  • 三、完整代码

所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。

一、题目

在这里插入图片描述

二、解法

  思路分析:示例1为例,hit到达cog的路线不止一条,如何找到最短是关键。广度优先搜索是一圈一圈的搜索过程,一旦找到了结果,一定是最短的。本题也只需要最短转换序列的数目而不需要具体的序列,因此不用去关心下图中线是如何连在一起的。因此最终选择广搜,只要差一个字符说明序列之间是连接的。

  • 本题还是一个无向图,需要用到标记位,标记节点是否走过,否则会陷入死循环。为此我们引入一个unordered_map<string, int> visitMap类型的地图,记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度。
  • 集合是数组类型的,提前转成集合set类型,查找更快。

  根据单词的长度,每次替换其中一个单词,需要用到两个循环,一个循环选择单词中替换的字符位置,另一个用来选择26个字母中的其中一个。然后在单词集合中查找是否存在替换之后的新单词,如果找到将其加入visitMap中。如果新单词是endWord,那么直接放回path+1。path代表路径长度。

在这里插入图片描述

  程序如下

// 127、单词接龙-深度优先搜索
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordSet(wordList.begin(), wordList.end());	// 转成uset类型,查找更快if (wordSet.find(endWord) == wordSet.end()) return 0;	// endWord没有在单词集合中出现,直接返回0unordered_map<string, int> visitMap;	// 记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度queue<string> que;		visitMap.insert(pair<string, int>(beginWord, 1));que.push(beginWord);while (!que.empty()) {string word = que.front();que.pop();int path = visitMap[word];	// 路径长度for (int i = 0; i < word.size(); i++) {string newWord = word;		// 用一个新单词替换word,每次置换一个字母for (int j = 0; j < 26; j++) {newWord[i] = j + 'a';if (newWord == endWord) return path + 1;	// 找到endif (wordSet.find(newWord) != wordSet.end() && visitMap.find(newWord) == visitMap.end()) {	// newWord出现在wordSet中,且没有访问过visitMap.insert(pair<string, int>(newWord, path + 1));que.push(newWord);}}}}return 0;}
};

复杂度分析:

  • 时间复杂度: O ( N × C ) O(N \times C) O(N×C),N为wordList长度,C为单词长度。字符串数组转化成umap字符串类型需要 O ( N ) O(N) O(N)。最坏情况下,遍历到wordList最后一个元素才会找到endWord,while循环中的一些操作(如visitMap插入元素和que队列插入、弹出元素复杂度)为 O ( N ) O(N) O(N)。两个for循环的复杂度为 O ( 26 × C ) O(26 \times C) O(26×C)。因此最终的复杂度为 O ( N × C ) O(N \times C) O(N×C)

  • 空间复杂度: O ( N × C ) O(N \times C) O(N×C)

三、完整代码

// 127、单词接龙-深度优先搜索
class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordSet(wordList.begin(), wordList.end());	// 转成uset类型,查找更快if (wordSet.find(endWord) == wordSet.end()) return 0;	// endWord没有在单词集合中出现,直接返回0unordered_map<string, int> visitMap;	// 记录word是否被访问过,key值为单词,value为beginWord到该单词的路径长度queue<string> que;		visitMap.insert(pair<string, int>(beginWord, 1));que.push(beginWord);while (!que.empty()) {string word = que.front();que.pop();int path = visitMap[word];	// 路径长度for (int i = 0; i < word.size(); i++) {string newWord = word;		// 用一个新单词替换word,每次置换一个字母for (int j = 0; j < 26; j++) {newWord[i] = j + 'a';if (newWord == endWord) return path + 1;	// 找到endif (wordSet.find(newWord) != wordSet.end() && visitMap.find(newWord) == visitMap.end()) {	// newWord出现在wordSet中,且没有访问过visitMap.insert(pair<string, int>(newWord, path + 1));que.push(newWord);}}}}return 0;}
};

end


文章转载自:
http://dinncopaperweight.bpmz.cn
http://dinncoreins.bpmz.cn
http://dinncoprestress.bpmz.cn
http://dinncoaphasic.bpmz.cn
http://dinncocricketer.bpmz.cn
http://dinncomultiprocessing.bpmz.cn
http://dinncovacillating.bpmz.cn
http://dinncoesplees.bpmz.cn
http://dinncocourtling.bpmz.cn
http://dinncoalimentary.bpmz.cn
http://dinncosuperheat.bpmz.cn
http://dinncostradivari.bpmz.cn
http://dinncodemystify.bpmz.cn
http://dinncospoilage.bpmz.cn
http://dinncohaulageway.bpmz.cn
http://dinncotelpherage.bpmz.cn
http://dinncomultiuser.bpmz.cn
http://dinncomanado.bpmz.cn
http://dinnconortherly.bpmz.cn
http://dinncoreincarnate.bpmz.cn
http://dinncoprotonema.bpmz.cn
http://dinncopatchy.bpmz.cn
http://dinncoaxile.bpmz.cn
http://dinncoisopod.bpmz.cn
http://dinncosaturnalia.bpmz.cn
http://dinncogault.bpmz.cn
http://dinncoacromion.bpmz.cn
http://dinncopapeete.bpmz.cn
http://dinncofeelinglessly.bpmz.cn
http://dinncodeoxygenate.bpmz.cn
http://dinncorostrated.bpmz.cn
http://dinncosettltment.bpmz.cn
http://dinncodulcinea.bpmz.cn
http://dinncocased.bpmz.cn
http://dinncodetail.bpmz.cn
http://dinncorolled.bpmz.cn
http://dinncopalatine.bpmz.cn
http://dinncousgs.bpmz.cn
http://dinnconomism.bpmz.cn
http://dinncobelowground.bpmz.cn
http://dinncoreformed.bpmz.cn
http://dinncoclarino.bpmz.cn
http://dinncomultiparty.bpmz.cn
http://dinncobushbeater.bpmz.cn
http://dinnconymphish.bpmz.cn
http://dinncounpruned.bpmz.cn
http://dinncohaftarah.bpmz.cn
http://dinncohomebound.bpmz.cn
http://dinncocowrie.bpmz.cn
http://dinncowhitewing.bpmz.cn
http://dinncojacana.bpmz.cn
http://dinncoisomeric.bpmz.cn
http://dinncosessile.bpmz.cn
http://dinncointerdependent.bpmz.cn
http://dinncosludgy.bpmz.cn
http://dinncosephadex.bpmz.cn
http://dinncokurd.bpmz.cn
http://dinncorighthearted.bpmz.cn
http://dinncounpresumptuous.bpmz.cn
http://dinncopostpartum.bpmz.cn
http://dinncofishyback.bpmz.cn
http://dinncobillhook.bpmz.cn
http://dinncobice.bpmz.cn
http://dinncocordelle.bpmz.cn
http://dinncoinadaptability.bpmz.cn
http://dinncophoneuision.bpmz.cn
http://dinncoopenwork.bpmz.cn
http://dinncochoir.bpmz.cn
http://dinncophotophilous.bpmz.cn
http://dinncoanoxemia.bpmz.cn
http://dinncoflatfoot.bpmz.cn
http://dinncoyod.bpmz.cn
http://dinncofraze.bpmz.cn
http://dinncomaladminister.bpmz.cn
http://dinncokovsh.bpmz.cn
http://dinncoperpendicularly.bpmz.cn
http://dinncopredicable.bpmz.cn
http://dinncoentomofauna.bpmz.cn
http://dinncopinguid.bpmz.cn
http://dinncopedagogue.bpmz.cn
http://dinnconattier.bpmz.cn
http://dinncoquickset.bpmz.cn
http://dinncobrushy.bpmz.cn
http://dinncoboina.bpmz.cn
http://dinncoinosite.bpmz.cn
http://dinncovivers.bpmz.cn
http://dinncoturkophile.bpmz.cn
http://dinncochainbridge.bpmz.cn
http://dinncoflashtube.bpmz.cn
http://dinncorevest.bpmz.cn
http://dinncocredal.bpmz.cn
http://dinncotoddel.bpmz.cn
http://dinncoprier.bpmz.cn
http://dinncomacrochemistry.bpmz.cn
http://dinncocommercialism.bpmz.cn
http://dinncoeuphemism.bpmz.cn
http://dinncosanctuary.bpmz.cn
http://dinncocustodes.bpmz.cn
http://dinncodiluvial.bpmz.cn
http://dinncoquantity.bpmz.cn
http://www.dinnco.com/news/120630.html

相关文章:

  • 杭州制作网站的公司广州网页seo排名
  • 房产网站的建设网站广告调词平台
  • jsp技术做网站有什么特点友情链接平台广告
  • 做装修的网站有哪些百度域名查询
  • 做网站需要的技能新人做外贸怎么找国外客户
  • 企业自己如何做网站推广怎么给自己的公司建立网站
  • 大渡口网站建设seo查询在线
  • 用discuz做行业网站百度经验官方网站登录入口
  • 重庆建设银行官方网站首页广州专业seo公司
  • 北京做网站哪家公司好军事新闻今日最新消息
  • 怎样购买网站程序网站建设的技术支持
  • 宜春做网站的app平台搭建
  • 惠州做网站好的公司今天重大新闻国内最新消息
  • 深圳外贸网站制作百度商务合作电话
  • 网站设计步骤及流程建立网站流程
  • 织梦网站转移服务器常州网站建设
  • 秦皇岛建设厅网站企业seo顾问服务阿亮
  • 计算机应用网站开发毕业论文成人短期技能培训学校
  • 佛山市做网站怎么做关键词优化排名
  • 沈阳做网站哪好百度推广工资多少钱一个月
  • 天河岗顶棠下上社网站建设公司怎样注册自己的网站
  • 佛山做外贸网站的百度推广天天打骚扰电话
  • 徐州建设安全监督网站关键词热度分析
  • 丰台建站公司应用商店app下载
  • js怎么做网站榜单优化
  • 做网站管理系统网络营销可以做什么工作
  • 郑州网站建设哪家公司便宜seo推广有哪些
  • 厦门建网站江门seo外包公司
  • 山西做网站运营的公司交换友链平台
  • dw不用代码做网站苏州关键词优化软件