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

郑州做旅游网站一站式营销平台

郑州做旅游网站,一站式营销平台,企业网络管理软件,中国建设银行企业信息门户网站trie树的使用场景 我们若需要制作一个通讯录的软件,使用常规树结构查询的复杂度为O(logn),但trie树的复杂度确与数据多少无关,与单词长度有关,这就大大缩减的查询的时间复杂度。 trie树的基本实现 基础结构 package com.study.trieDemo;i…

trie树的使用场景

我们若需要制作一个通讯录的软件,使用常规树结构查询的复杂度为O(logn),但trie树的复杂度确与数据多少无关,与单词长度有关,这就大大缩减的查询的时间复杂度。

trie树的基本实现

基础结构

package com.study.trieDemo;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class Trie {private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}private Node root;private int size;public int getSize() {return size;}}

添加

   /*** 向tire中添加一个单词* @param word*/public void add(String word) {Node cur = root;for (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (cur.next.get(c) == null)cur.next.put(c, new Node());cur = cur.next.get(c);}if (!cur.isWord) {cur.isWord = true;size++;}}

搜索单词

  /*** 查找单词word是否在trie树中* @param word* @return*/public boolean contains(String word){Node cur=root;for (int i = 0; i <word.length() ; i++) {char c=word.charAt(i);if (cur.next.get(c)==null)return false;cur=cur.next.get(c);}return cur.isWord;}

leetcode中关于trie的题目

208. 实现 Trie (前缀树)

208. 实现 Trie (前缀树)

实现

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class Trie_208 {private Node root;private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}/*** Initialize your data structure here.*/public Trie_208() {root = new Node();}/*** Inserts a word into the trie.*/public void insert(String word) {Node cur = root;for (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (cur.next.get(c) == null)cur.next.put(c, new Node());cur = cur.next.get(c);}cur.isWord = true;}/*** Returns if the word is in the trie.*/public boolean search(String word) {Node cur = root;for (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (cur.next.get(c) == null)return false;cur = cur.next.get(c);}return cur.isWord;}/*** Returns if there is any word in the trie that starts with the given prefix.*/public boolean startsWith(String prefix) {Node cur = root;for (int i = 0; i < prefix.length(); i++) {char c = prefix.charAt(i);if (cur.next.get(c) == null)return false;cur = cur.next.get(c);}return true;}
}

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

题目链接

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

实现

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class WordDictionary_211 {private Node root;private class Node {private boolean isWord;private TreeMap<Character, Node> next;public Node(boolean isWord) {this.isWord = isWord;next = new TreeMap<Character, Node>();}public Node() {this(false);}}/*** Initialize your data structure here.*/public WordDictionary_211() {root = new Node();}/*** Adds a word into the data structure.*/public void addWord(String word) {Node cur = root;for (int i = 0; i < word.length(); i++) {char c = word.charAt(i);if (cur.next.get(c) == null)cur.next.put(c, new Node());cur = cur.next.get(c);}cur.isWord = true;}/*** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.*//*** 使用match进行递归查询* @param word* @return*/public boolean search(String word) {return match(root, word, 0);}private boolean match(Node node, String word, int index) {if (index == word.length())return node.isWord;char c = word.charAt(index);if (c != '.') {if (node.next.get(c) == null)return false;return match(node.next.get(c), word, index + 1);} else {for (char nextChar : node.next.keySet())if (match(node.next.get(nextChar), word, index + 1))return true;return false;}}}

677. 键值映射

题目链接

677. 键值映射

题解

package com.study.leetcode;import java.util.TreeMap;/*** Created by Zsy on 2020/8/21.*/
public class MapSum_677 {private class Node {public int value;public TreeMap<Character, Node> next;public Node(int value) {this.value = value;next = new TreeMap<Character, Node>();}public Node() {this(0);}}private Node root;/*** Initialize your data structure here.*/public MapSum_677() {root = new Node();}/*** 非递归法插入节点,通过查询next中是否存在对应key的节点,进行相应插入操作* @param key* @param val*/public void insert(String key, int val) {Node cur = root;for (int i = 0; i < key.length(); i++) {char c = key.charAt(i);if (cur.next.get(c) == null)cur.next.put(c, new Node());cur = cur.next.get(c);}cur.value = val;}/*** 找到匹配节点的指针,将地址传给sum进行递归计算* @param prefix* @return*/public int sum(String prefix) {Node cur = root;for (int i = 0; i < prefix.length(); i++) {char c = prefix.charAt(i);if (cur.next.get(c) == null)return 0;cur = cur.next.get(c);}return sum(cur);}private int sum(Node node) {//省略了if(node==null) return 0;int value = node.value;for (char c : node.next.keySet()) {value+=sum(node.next.get(c));}return value;}
}
http://www.mmbaike.com/news/62758.html

相关文章:

  • 网站建设前后台语言郑州seo技术服务
  • 有了域名和空间怎么做网站内容国外新闻最新消息
  • jsp web网站开发企业网站推广的一般策略
  • vs2013做网站宁波网络营销推广公司
  • 贵州城乡住房建设厅网站自媒体培训
  • 北京建设银行公积金提取网站网络营销案例ppt
  • webportal自助建站百度如何投放广告
  • 怎么看公司网站做的好不好哦营业推广的概念
  • 桂林网站建设公司微信社群营销怎么做
  • 宁波网站开发公司千锋教育培训多少钱费用
  • 右翼网站如何做好宣传推广
  • 建设公关型的企业网站有哪些优化大师电脑版官网
  • 响应式相册网站百度软件
  • 常德网站建设wynet123公司官网开发制作
  • 天津网站开发培训企业网站的作用
  • 东莞市建设工程网站东莞新闻头条新闻
  • 福州网站建设搭建怎么在百度推广自己的公司
  • 珞珈学子网站建设互联网产品运营推广方案
  • 节日网站网页设计html深圳网络推广推荐
  • 网站推广怎么做优化广告投放公司
  • 团购网站 模板人员优化方案
  • 网站开发 面试营销案例分析
  • 学院招生网站建设方案企业邮箱网页版
  • 福州做网站建设公司南京seo优化
  • 网站开发方案ppt游戏代理加盟平台
  • 会议网站冯宗耀seo教程
  • 广州白云区疫情实时报告网站seo源码
  • 佛山顺德做网站产品宣传
  • 有做门窗找活的网站吗百度导航如何设置公司地址
  • 做微商推广有哪些好的分类信息网站企业站seo外包