leetcode--Implement Trie (Prefix Tree)
2015-06-20 20:51
375 查看
Implement a trie with
Note:
You may assume that all inputs are consist of lowercase letters
// Initialize your data structure here.
public TrieNode() {}
Map<Character,TrieNode> next = new HashMap<Character,TrieNode>();
char c='\0';
boolean isEnd = false;
public TrieNode(char c) {
this.c = c;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode cur = root;
for(int i=0;i<word.length();i++){
if(!cur.next.containsKey(word.charAt(i))){
cur.next.put(word.charAt(i), new TrieNode(word.charAt(i)));
}
cur = cur.next.get(word.charAt(i));
}
cur.isEnd = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode cur = root;
for(int i=0;i<word.length();i++){
if(cur.next.containsKey(word.charAt(i))){
cur = cur.next.get(word.charAt(i));
}else{
return false;
}
}
if(cur.isEnd) return true;
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode cur = root;
for(int i=0;i<prefix.length();i++){
if(cur.next.containsKey(prefix.charAt(i))){
cur = cur.next.get(prefix.charAt(i));
}else{
return false;
}
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
insert,
search, and
startsWithmethods.
Note:
You may assume that all inputs are consist of lowercase letters
a-z.class TrieNode {
// Initialize your data structure here.
public TrieNode() {}
Map<Character,TrieNode> next = new HashMap<Character,TrieNode>();
char c='\0';
boolean isEnd = false;
public TrieNode(char c) {
this.c = c;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode cur = root;
for(int i=0;i<word.length();i++){
if(!cur.next.containsKey(word.charAt(i))){
cur.next.put(word.charAt(i), new TrieNode(word.charAt(i)));
}
cur = cur.next.get(word.charAt(i));
}
cur.isEnd = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode cur = root;
for(int i=0;i<word.length();i++){
if(cur.next.containsKey(word.charAt(i))){
cur = cur.next.get(word.charAt(i));
}else{
return false;
}
}
if(cur.isEnd) return true;
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode cur = root;
for(int i=0;i<prefix.length();i++){
if(cur.next.containsKey(prefix.charAt(i))){
cur = cur.next.get(prefix.charAt(i));
}else{
return false;
}
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
相关文章推荐
- 【swift总结】基本的操作运行符
- 【Leetcode】Contains Duplicate 1 and 2
- 机房重构之存储过程
- 爬虫 —— 布隆过滤器算法(Bloom Filter)
- 进程的概念和特征
- Android关于TextView 宽度过大导致Drawable无法居中问题
- shell知识点
- 【组合】 达到了继承的效果,又不破坏封装
- 计算cell的高度和自动适配高度
- 常用希腊字母总结
- Hadoop简介与hadoop2.4.1伪分布式搭建
- 《C++专项练习》 — (2)
- 《C++专项练习》 — (2)
- ACM 头文件与宏定义 (以后有好的会随时更新)
- Redis Sentinel机制与用法[转]
- 矩阵常用归一化
- 隐藏系统保留区
- Socket剖析
- Python MySQLdb Linux下安装笔记
- android如何将一个字符串生成二维码