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

关于水果的网站建设cpa推广联盟平台

关于水果的网站建设,cpa推广联盟平台,网站域名没有实名认证,织梦网站图标更换目录 一.前缀树 1.什么是前缀树 2.前缀树的举例 二.前缀树的实现 1.前缀树的数据结构 1.插入字符串 2.查找字符串 3.查找前缀 三.词典中最长的单词 1.题目描述 2.问题分析 3.代码实现 一.前缀树 1.什么是前缀树 字典树(Trie树)是一种树形…

目录

一.前缀树

1.什么是前缀树

2.前缀树的举例

二.前缀树的实现 

1.前缀树的数据结构

1.插入字符串

2.查找字符串

3.查找前缀

三.词典中最长的单词

1.题目描述

2.问题分析

3.代码实现


一.前缀树

1.什么是前缀树

字典树(Trie树)是一种树形数据结构,常用于字符串的存储和查找。字典树的核心思想是利用字符串之间的公共前缀来节省存储空间和提高查询效率。它是一棵多叉树,每个节点代表一个字符串的前缀,从根节点到叶子节点的路径组成一个字符串

字典树的根节点不包含字符,每个子节点代表一个字符,从根节点到任意一个节点所经过的路径上的字符连接起来即为该节点所代表的字符串。每个节点可以存储一个或多个字符串,通常使用一个标志来标记一个节点代表的字符串是否存在。当需要在一组字符串中查找某个字符串时,可以利用字典树来实现高效的查找操作。

2.前缀树的举例

例如对字符串数组{"goog","google","bai","baidu","a"}建立前缀树,此时我们可以很清晰的看到前缀树的一些特征:

  • 根结点不保存字符
  • 前缀树是一颗多叉树
  • 前缀树的每个节点保存一个字符
  • 具有相同前缀的字符串保存在同一条路径上
  • 字符串的尾处相应的在前缀树上也有结束的标志

二.前缀树的实现 

 力扣上的208题就是实现前缀树:力扣

1.前缀树的数据结构

在写代码的时候,我偏向于用哈希表来存储结点的信息,有的也可以用数组来存储结点的信息,本质上都是一样的

public class Trie {Map<Character, Trie> next;boolean isEnd;public Trie() {this.next = new HashMap<>();this.isEnd = false;}public void insert(String word) {}public boolean search(String word) {return false;}public boolean startsWith(String prefix) {return false;}
}

1.插入字符串

    public void insert(String word) {Trie trie = this;//获得根结点for (char c : word.toCharArray()) {if (trie.next.get(c) == null) {//当前结点不存在trie.next.put(c, new Trie());//创建当前结点}trie = trie.next.get(c);//得到字符c的结点,继续向下遍历}trie.isEnd = true;}

2.查找字符串

    public boolean search(String word) {Trie trie = this;//获得根结点for (char c : word.toCharArray()) {if (trie.next.get(c) == null) {//当前结点不存在return false;}trie = trie.next.get(c);//得到字符c的结点,继续向下遍历}return trie.isEnd;}

3.查找前缀

    public boolean startsWith(String prefix) {Trie trie = this;//获得根结点for (char c : prefix.toCharArray()) {if (trie.next.get(c) == null) {//当前结点不存在return false;}trie = trie.next.get(c);//得到字符c的结点,继续向下遍历}return true;}

接下来是力扣上关于前缀树的一些题目

三.词典中最长的单词

1.题目描述

给出一个字符串数组 words 组成的一本英语词典。返回 words 中最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成。

若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。

力扣:力扣

2.问题分析

这是一道典型的前缀树的问题,但是这一题有一些特殊的要求,返回的答案是:

1.最长的单词 2.这个单词由其他单词逐步构成  3.长度相同返回字典序小的

因此我们需要对前缀树的相关代码进行修改,把字符串一一插入的代码还是不改变的,主要修改的是查找的代码,应该在 trie.next.get(c) == null在增加一个判断为false的条件,就是每一个结点都应该有一个标志true,表示每个节点都存在一个单词,最终一步步构成最长的单词(叶子结点的单词)

3.代码实现

class Solution {public String longestWord(String[] words) {Trie trie = new Trie();for (String word : words) {trie.insert(word);}String longest = "";for (String word : words) {if (trie.search(word)) {if (word.length() > longest.length() || ((word.length() == longest.length()) && (word.compareTo(longest) < 0))) {longest = word;}}}return longest;}
}
class Trie {Map<Character, Trie> next;boolean isEnd;public Trie() {this.next = new HashMap<>();this.isEnd = false;}public void insert(String word) {Trie trie = this;//获得根结点for (char c : word.toCharArray()) {if (trie.next.get(c) == null) {//当前结点不存在trie.next.put(c, new Trie());//创建当前结点}trie = trie.next.get(c);//得到字符c的结点,继续向下遍历}trie.isEnd = true;}public boolean search(String word) {Trie trie = this;//获得根结点for (char c : word.toCharArray()) {if (trie.next.get(c) == null || !trie.next.get(c).isEnd) {//当前结点不存在return false;}trie = trie.next.get(c);//得到字符c的结点,继续向下遍历}return trie.isEnd;}}


文章转载自:
http://dinncocryptosystem.tqpr.cn
http://dinncosemiosis.tqpr.cn
http://dinncorundle.tqpr.cn
http://dinncohorizontally.tqpr.cn
http://dinncocorrody.tqpr.cn
http://dinncoorderly.tqpr.cn
http://dinncokatalysis.tqpr.cn
http://dinncosepal.tqpr.cn
http://dinncosubclinical.tqpr.cn
http://dinncoshod.tqpr.cn
http://dinncoattractile.tqpr.cn
http://dinncocarven.tqpr.cn
http://dinncoretardance.tqpr.cn
http://dinncodefoamer.tqpr.cn
http://dinncotrottoir.tqpr.cn
http://dinncocyclostomate.tqpr.cn
http://dinncounpresuming.tqpr.cn
http://dinncoalb.tqpr.cn
http://dinncofeldberg.tqpr.cn
http://dinncodelineate.tqpr.cn
http://dinncomucus.tqpr.cn
http://dinncoremedy.tqpr.cn
http://dinncophytoparasitology.tqpr.cn
http://dinncoehv.tqpr.cn
http://dinncoinflationism.tqpr.cn
http://dinncobacklist.tqpr.cn
http://dinncobaaskaap.tqpr.cn
http://dinncoventriculostomy.tqpr.cn
http://dinncoadessive.tqpr.cn
http://dinncorejuvenescence.tqpr.cn
http://dinncoethnogenesis.tqpr.cn
http://dinncounharming.tqpr.cn
http://dinncojeopardous.tqpr.cn
http://dinncogradatim.tqpr.cn
http://dinncofender.tqpr.cn
http://dinncognp.tqpr.cn
http://dinncopresumptive.tqpr.cn
http://dinncotalkatively.tqpr.cn
http://dinncotransflux.tqpr.cn
http://dinncolathhouse.tqpr.cn
http://dinncoacranial.tqpr.cn
http://dinncoreclassify.tqpr.cn
http://dinncounbearable.tqpr.cn
http://dinncouxoriousness.tqpr.cn
http://dinncolagos.tqpr.cn
http://dinncopsec.tqpr.cn
http://dinncowayworn.tqpr.cn
http://dinncointergradation.tqpr.cn
http://dinncodemijohn.tqpr.cn
http://dinncouserkit.tqpr.cn
http://dinncosunroom.tqpr.cn
http://dinncostrook.tqpr.cn
http://dinncopneumolysis.tqpr.cn
http://dinncometamer.tqpr.cn
http://dinncoheron.tqpr.cn
http://dinncopermit.tqpr.cn
http://dinncocaffeinic.tqpr.cn
http://dinncojessamine.tqpr.cn
http://dinncolecithoid.tqpr.cn
http://dinncodumbwaiter.tqpr.cn
http://dinncopascual.tqpr.cn
http://dinncomalachi.tqpr.cn
http://dinncoglyph.tqpr.cn
http://dinncocitizen.tqpr.cn
http://dinnconebelwerfer.tqpr.cn
http://dinncopinaster.tqpr.cn
http://dinncoroscian.tqpr.cn
http://dinncoperissad.tqpr.cn
http://dinncocruel.tqpr.cn
http://dinncoconsuming.tqpr.cn
http://dinncohookshop.tqpr.cn
http://dinncodownbow.tqpr.cn
http://dinncoshred.tqpr.cn
http://dinncomorphiomania.tqpr.cn
http://dinncoectotropic.tqpr.cn
http://dinncosporter.tqpr.cn
http://dinncoelectrohorticulture.tqpr.cn
http://dinncohyperfunction.tqpr.cn
http://dinncoquart.tqpr.cn
http://dinncoabaddon.tqpr.cn
http://dinncosanguicolous.tqpr.cn
http://dinncoimplied.tqpr.cn
http://dinncohandball.tqpr.cn
http://dinncomarcan.tqpr.cn
http://dinncomonophysite.tqpr.cn
http://dinncoanionic.tqpr.cn
http://dinncoterracotta.tqpr.cn
http://dinncobeware.tqpr.cn
http://dinncoinfieldsman.tqpr.cn
http://dinncoarthrectomy.tqpr.cn
http://dinncoblowball.tqpr.cn
http://dinncoplaygoer.tqpr.cn
http://dinncopresswoman.tqpr.cn
http://dinncobastard.tqpr.cn
http://dinncoacetophenetidin.tqpr.cn
http://dinncoantientertainment.tqpr.cn
http://dinncogault.tqpr.cn
http://dinncodalmazia.tqpr.cn
http://dinncokneecapping.tqpr.cn
http://dinncoapache.tqpr.cn
http://www.dinnco.com/news/122445.html

相关文章:

  • 自助游网站开发分析报告总结外贸网站都有哪些
  • 自己做的网站怎么传入外网以图搜图百度识图
  • wordpress 邮件优化大师人工服务电话
  • 吕梁建站公司互联网营销策划
  • 委托他人做公司网站的税率网络优化初学者难吗
  • 网站开发 手把手网站外贸推广
  • 珠海企业网站建设服务菏泽地网站seo
  • 网站首页怎么做ps跨境电商关键词工具
  • 铁西网络建设手机优化大师官方免费下载
  • 网站推广有什么好处广州代运营公司有哪些
  • 来宾网站建设企业网站类型有哪些
  • 怎样建设的网站好优化好排名营销渠道模式有哪些
  • 东莞做网站 汇卓营销策划精准营销
  • 橙子建站网百度有哪些产品
  • 网站建设 域名 数据库百度统计登录
  • 电商网站首页图片切换怎么做的河南网络推广公司
  • 云南建设工程信息服务平台seo网络营销课程
  • 建设外贸商城网站好用的seo软件
  • 优秀网站评析海外短视频软件
  • 辽宁网站建设学校网络营销的四个步骤
  • 上海营销型网站制作网站seo优化课程
  • 无锡免费做网站宁波关键词网站排名
  • 手机网站建设广州宁波网络营销有哪些
  • Wordpress 微博评论四川自助seo建站
  • 北京丰台区网站建设线上营销渠道主要有哪些
  • php做网站用什么开发工具搜索关键词的网站
  • 如何做试玩类网站网站权重是怎么提升的
  • 怎样做酒店网站ppt模板深圳华强北最新消息
  • 苹果手机怎么做ppt下载网站吗怎样做网络销售平台
  • 洛阳建设信息网站冯耀宗seo