[LeetCode]Implement Trie(Prefix Tree),解题报告
来源:互联网 发布:js位运算或 编辑:程序博客网 时间:2024/05/17 06:32
目录
- 目录
- 概述
- Trie树基本实现
- 定义Trie树节点
- 添加操作
- 查询word是否在Trie树中
- AC完整代码
概述
Trie树,又称为字典树、单词查找树或者前缀树,是一种用于快速检索的多叉数结构。例如,英文字母的字典树是26叉数,数字的字典树是10叉树。
Trie树的基本性质有三点,归纳为:
- 根节点不包含字符,根节点外每一个节点都只包含一个字符。
- 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
- 每个节点的所有子节点包含的字符串不相同。
Trie树基本实现
我们通过LeetCode的上的一道Trie题目来描述Trie树的实现。Implement Trie(Prefix Tree)。
定义Trie树节点
class TrieNode { boolean isWord; HashMap<Character, TrieNode> nexts; public TrieNode() { nexts = new HashMap<Character, TrieNode>(); }}
添加操作
我们向Trie树中添加一个字符串word,具体步骤如下:
// Inserts a word into the trie. public void insert(String word) { char[] s = word.toCharArray(); TrieNode p = root; int i = 0, n = s.length; // traverse existing while (i < n) { TrieNode next = p.nexts.get(s[i]); if (next != null) { p = next; i ++; } else { break; } } // append new nodes while (i < n) { TrieNode newTrie = new TrieNode(); p.nexts.put(s[i], newTrie); p = newTrie; i ++; } // set word end p.isWord = true; }
查询word是否在Trie树中
// Returns if the word is in the trie. public boolean search(String word) { TrieNode p = root; for (int i = 0; i < word.length(); i ++) { TrieNode child = p.nexts.get(word.charAt(i)); if (child == null) { return false; } p = child; } return p.isWord; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { TrieNode p = root; for (int i = 0; i < prefix.length(); i ++) { TrieNode child = p.nexts.get(prefix.charAt(i)); if (child == null) { return false; } p = child; } return true; }
AC完整代码
import java.util.HashMap;class TrieNode { boolean isWord; HashMap<Character, TrieNode> nexts; public TrieNode() { nexts = new HashMap<Character, TrieNode>(); }}public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { char[] s = word.toCharArray(); TrieNode p = root; int i = 0, n = s.length; // traverse existing while (i < n) { TrieNode next = p.nexts.get(s[i]); if (next != null) { p = next; i ++; } else { break; } } // append new nodes while (i < n) { TrieNode newTrie = new TrieNode(); p.nexts.put(s[i], newTrie); p = newTrie; i ++; } // set word end p.isWord = true; } // Returns if the word is in the trie. public boolean search(String word) { TrieNode p = root; for (int i = 0; i < word.length(); i ++) { TrieNode child = p.nexts.get(word.charAt(i)); if (child == null) { return false; } p = child; } return p.isWord; } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { TrieNode p = root; for (int i = 0; i < prefix.length(); i ++) { TrieNode child = p.nexts.get(prefix.charAt(i)); if (child == null) { return false; } p = child; } return true; } public static void main(String[] args) { Trie trie = new Trie(); trie.insert("keydsdsds"); System.out.println(trie.startsWith("key")); }}
0 0
- [LeetCode]Implement Trie(Prefix Tree),解题报告
- [leetcode] 208. Implement Trie (Prefix Tree) 解题报告
- [Leetcode] 208. Implement Trie (Prefix Tree) 解题报告
- [leetcode] Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- [leetcode]Implement Trie (Prefix Tree)
- [LeetCode] Implement Trie (Prefix Tree)
- leetcode:Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- leetcode Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- [leetcode] Implement Trie (Prefix Tree)
- [leetcode] Implement Trie (Prefix Tree)
- [LeetCode]Implement Trie (Prefix Tree)
- Leetcode Implement Trie (Prefix Tree)
- leetcode--Implement Trie (Prefix Tree)
- LeetCode: Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- 让delphi程序不受WINDOWS日期格式的影响
- Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- Oracle外连接中对非连接条件使用(+)需要注意的地方
- froglt教你使用色相环配色(原创理论)
- [LeetCode]Implement Trie(Prefix Tree),解题报告
- 设计模式(十七)访问者模式(Visitor)-行为型
- 路由器】路由器3G类异常,即3G业务不定时中断,造成过一段时间后业务可以自动恢复,或者必须通过重启路由器等操作业务才能够恢复
- iOS中的自定义通知和通知传值的简单介绍
- 【翻译】Android Support Library Features(二)
- laravel 路由
- Sales Order Related Scripts
- echarts 实例(一:动态数据)
- 全局组合按键-老板键