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

211. 添加与搜索单词 - 数据结构设计---------------字典树

211. 添加与搜索单词 - 数据结构设计

  • 原题链接:
  • 完成情况:
  • 解题思路:
  • 参考代码:

原题链接:

211. 添加与搜索单词 - 数据结构设计

https://leetcode.cn/problems/design-add-and-search-words-data-structure/description/

完成情况:

在这里插入图片描述

解题思路:

参考代码:

package 中等题;public class __211添加与搜索单词_数据结构设计 {class WordDictionary {private Trie root;public WordDictionary() {root  = new Trie();}public void addWord(String word) {root.insert(word);}public boolean search(String word) {return dfs(word,0,root);}/**** @param word* @param index* @param curNode* @return*/private boolean dfs(String word, int index, Trie curNode) {if (index == word.length()){return curNode.isEnd();       //curNode.isEnd是去调Trie里面的isEnd属性}char ch = word.charAt(index);if (Character.isLetter(ch)){        //如果已经包含当前字典序开头元素int childIndex = ch - 'a';/**** 下面这行代码。他妈的是什么东西啊????????????**/Trie child = curNode.getChildren()[childIndex];/*********/if (child != null && dfs(word,index+1,child)){return true;}}else {for (int i=0;i<26;i++){Trie child = curNode.getChildren()[i];if (child != null  && dfs(word,index+1,child)){return true;}}}return false;}}//创建一个【字典树】类class Trie{private Trie[] children;private  boolean isEnd;public Trie(){children = new Trie[26];    //字典树链表位26个字母isEnd = false;  //每个都默认看看是不是尾部}/**** @param word*/public void insert(String word){Trie node = this;for (int i=0;i<word.length();i++){char ch = word.charAt(i);int  index = ch -'a';       //用数字带指代26个字母索引if (node.children[index] == null){node.children[index] = new Trie();}node = node.children[index];}node.isEnd = true;}/***  生成对应的默认构造器*/public Trie[] getChildren(){return children;}public boolean isEnd() {return isEnd;}}}
http://www.lryc.cn/news/104363.html

相关文章:

  • SQL Server通过指令备份数据库和恢复数据库
  • windows如何上架ios应用到app store
  • Hadoop学习日记-YARN组件
  • 汽车过户时,怎么选到理想的好车牌?
  • 力扣468 验证IP地址
  • 前端静态登录页面实现
  • 华为数通HCIA-网络参考模型(TCP/IP)
  • java快速生成数据库表文档(HTML、DOC、MD)
  • Dojo学习和常用知识
  • 媒体查询详解
  • 华为数通HCIP-IGMP(网络组管理协议)
  • 价格管控有哪些有效的方法
  • 【Docker】Docker相关基础命令
  • 掌握Python的X篇_16_list的切片、len和in操作
  • 给定长度值length,把列表切分成每段长度为length的N段列表,Kotlin
  • leetcode每日一题Day2——344. 反转字符串
  • ISP记1
  • 无线蓝牙耳机有什么值得耳机买的?几款值得买的口碑品牌盘点
  • 异步检索在 Elasticsearch 中的理论与实践
  • 了解Unity编辑器之组件篇Physics 2D(十二)
  • [Pytorch]手写数字识别——真·手写!
  • android studio 找不到符号类 Canvas 或者 错误: 程序包java.awt不存在
  • AWS——02篇(AWS之服务存储EFS在Amazon EC2上的挂载——针对EC2进行托管文件存储)
  • FFmpeg 打包mediacodec 编码帧 MPEGTS
  • 软件测试如何推进项目进度?
  • 首次尝试鸿蒙开发!
  • 前端面试题-react
  • EIP-2535 Diamond standard 实用工具分享
  • 【LangChain】向量存储(Vector stores)
  • Debian/Ubuntu 安装 Chrome 和 Chrome Driver 并使用 selenium 自动化测试