(medium)LeetCode .Implement Trie (Prefix Tree)
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
思路:实现单词查找树,它是一种树形结构;用于保存大量的字符串。它的优点是:利用字符串的公共前缀来节约存储空间。每个节点有26个从节点。isEndOfWord,children,两个关键变量。
代码如下:
class TrieNode {
// Initialize your data structure here.
boolean isEndOfWord;
TrieNode[] children;
public TrieNode() {
this.isEndOfWord=false;
this.children=new TrieNode[26];
} } public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
TrieNode runner=root;
for(char c:word.toCharArray()){
if(runner.children[c-'a']==null){
runner.children[c-'a']=new TrieNode();
}
runner=runner.children[c-'a'];
}
runner.isEndOfWord=true;
} // Returns if the word is in the trie.
public boolean search(String word) {
TrieNode runner=root;
for(char c:word.toCharArray()){
if(runner.children[c-'a']==null){
return false;
}else{
runner=runner.children[c-'a'];
}
}
return runner.isEndOfWord;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode runner=root;
for(char c:prefix.toCharArray()){
if(runner.children[c-'a']==null){
return false;
}else{
runner=runner.children[c-'a'];
}
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
运行结果:
(medium)LeetCode .Implement Trie (Prefix Tree)的更多相关文章
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- [LeetCode] Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- LeetCode——Implement Trie (Prefix Tree)
Description: Implement a trie with insert, search, and startsWith methods. Note:You may assume that ...
- LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)
题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
- js给定时器调用传递参数
给定时器调用传递参数 无论是window.setTimeout 还是window.setInterval,在使用函数名作为调用句柄时都不 能带参数,而在许多场合必需要带参数,这就需要想方法解决.例如对 ...
- Jquery 使用小结
JQuery API中文档地址:http://www.hemin.cn/jq/index.html JQuery 中文社区:http://www.jquery.org.cn/ 1.siblings() ...
- 初识MariaDB存储引擎
在看MariaDB的存储引擎之前,可以先了解MySQL存储引擎. MySQL常用的存储引擎: MyISAM存储引擎:是MySQL的默认存储引擎.MyISAM不支持事务.也不支持外键,但其访问速度快,对 ...
- R(四): R开发实例-map分布图
前几章对R语言的运行原理.基本语法.数据类型.环境部署等基础知识作了简单介绍,本节将结合具体案例进行验证测试. 案例场景:从互联网下载全国三甲医院数据,以地图作为背景,展现各医院在地图上的分布图.全国 ...
- Object-c-数组的使用
一.数组: 1.数组初始化: a.NSArray *array = [[NSArray alloc] init]; b.NSArray *array = [[NSArray array]; 2.初始化 ...
- js键盘事件全面控制详解【转】
js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...
- 高性能MySQL --- 读书笔记(2) - 2016/8/2
第1章 MySQL架构 MySQL架构与其他数据库服务器大不相同,这使它能够适应广泛的应用.MySQL足够灵活,能适应高要求架构.例如Web应用,同时还适用于嵌入式应用.数据仓库.内容索引和分发软件. ...
- 【Spring-AOP-学习笔记-7】@Around增强处理简单示例
阅读目录 简单介绍 章节1:项目结构 章节2:定义切面类.连接点注解类 章节3:为待增强的方法--添加注解声明 章节4:AspectJ配置文件 章节5:测试类xxx 章节6:测试结果 Around 增 ...
- 128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 204. Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. ============= 找质数 ...