Implement Trie (Prefix Tree) ——LeetCode
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
实现一个字典树。
好久不做题,没感觉啊,TreeNode用一个布尔变量表示是否是一个合法单词的结尾即可,一开始还用cnt来计数,search的时候比较麻烦。
class TrieNode {
// Initialize your data structure here.
TrieNode [] next;
boolean valid;
public TrieNode() {
next = new TrieNode[26];
valid=false;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr.next[c-'a']==null){
ptr.next[c-'a'] = new TrieNode();
}
ptr=ptr.next[c-'a'];
}
ptr.valid=true;
} // Returns if the word is in the trie.
public boolean search(String word) {
int last = 0;
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return ptr.valid;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode ptr = root;
for(char c:prefix.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
Implement Trie (Prefix Tree) ——LeetCode的更多相关文章
- Implement Trie (Prefix Tree) - LeetCode
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) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树(查找树) 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 ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- Leetcode: Implement Trie (Prefix Tree) && Summary: Trie
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
随机推荐
- js正则实现用户输入银行卡号的控制及格式化
//js正则实现用户输入银行卡号的控制及格式化 <script language="javascript" type="text/javascript"& ...
- GoogleAuthenticator
<?php /** * PHP Class for handling Google Authenticator 2-factor authentication * * @author Micha ...
- Index Full Scan vs Index Fast Full Scan-1103
[Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...
- 【转】 UINavigationItem UINavigationBar 关系分析
原文:http://blog.csdn.net/luoyeffcs/article/details/16106707 目录 1.关系分析 2.关系综述 3.概念点 4.疑问 1.关系分析 UIBarI ...
- 段落排版--缩进(text-indent)
中文文字中的段前习惯空两个文字的空白,这个特殊的样式可以用下面代码来实现: p{text-indent:2em;} <p>1922年的春天,一个想要成名名叫尼克卡拉威(托比?马奎尔Tobe ...
- cocos2dx解析lua table数据结构 简易版.
之前一直用xml填配置, cocos2dx自带了xml解析接口, 非常方便. 但是, 接口好用也改变不了xml的结构字符太多, 书写麻烦, 乱七八糟的事实. 很早就想换lua, 无奈引擎没有现成接口, ...
- javascript——处理(获取)浏览器版本、操作系统
javascript——处理(获取)浏览器版本.操作系统 /** * Created by Administrator on 15-1-12. */ function BroswerUtil() { ...
- 『重构--改善既有代码的设计』读书笔记----Hide Delegate
所谓委托关系,就是一个类对于另一个类来说纯粹作为接口转发,即客户通过一个委托类去调用另一个对象.直白的委托关系就是委托类直接返回出目标类给客户调用,这个关系很麻烦,因为委托关系的变动就会影响客户端的代 ...
- javascript中的基本数据类型
在javascipt中有五大基本数据类型,列表如下: 1.数字 他们又又包括(正负整数,浮点数)十进制数,十六进制数与八进制数,指数和特殊数值NaN,(Infinity,-Infinity)正负无穷 ...
- memcache 操作类
<?php /** * memcache 操作实现 * @author timeless */ class Memcache_manage { //CI原始的信息 private $_ci; p ...