Description:

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

实现一个简单的Trie树,参考我的博客:http://www.cnblogs.com/wxisme/p/4876197.html

 class TrieNode {
public TrieNode[] son;
public boolean isEnd;
// Initialize your data structure here.
public TrieNode() {
this.son = new TrieNode[26];
isEnd = false; }
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
char[] wordChars = word.toCharArray();
TrieNode node = this.root;
for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
node.son[pos] = new TrieNode();
}
node = node.son[pos];
}
node.isEnd = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
char[] wordChars = word.toCharArray();
TrieNode node = this.root;
for(char ch : wordChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return false;
}
node = node.son[pos];
}
return node.isEnd;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) { char[] prefixChars = prefix.toCharArray();
TrieNode node = this.root;
for(char ch : prefixChars) {
int pos = ch - 'a';
if(node.son[pos] == null) {
return false;
}
node = node.son[pos];
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

LeetCode——Implement Trie (Prefix Tree)的更多相关文章

  1. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. [LeetCode] Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. (medium)LeetCode .Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  5. LeetCode Implement Trie (Prefix Tree) (实现trie树3个函数:插入,查找,前缀)

    题意:实现trie树的3个功能,只含小写字母的串. 思路:老实做即可! class TrieNode { public: TrieNode* chd[]; bool flag; // Initiali ...

  6. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  7. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  8. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  9. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

随机推荐

  1. Blog

    http://www.cnblogs.com/digdeep/archive/2015/11/16/4968453.htmlhttp://it.dataguru.cn/article-8406-1.h ...

  2. andorid ndk 各种坑啊 记录下

    android jni代码回调java的问题 因为多线程原因会导致找不到java类,无法call函数的问题 问题1找不到java类 在JNI_OnLoad的时候 保存下来 JNIEXPORT jint ...

  3. js中找string中重复项最多的字符个数

    // split():字符串中的方法,把字符串转成数组. // sort():数组中的排序方法,按照ACALL码进行排序. // join():数组中的方法,把数组转换为字符串 function de ...

  4. ecshop You don't have permission to access / on this server

    回复 6# 晓天 确实是这个短标签的事情,谢谢了啊. 第一种方法:替换程序里的内容,以后就省心了. 针对所有的php脚本 在DW里面运行查找替换l  主要做替换操作 当然是短标签替换为整标签 注意顺序 ...

  5. git默认忽略文件的大小写

  6. CocoaPods:说点关于它的

    CocoaPods安装和使用教程 安装及使用方法,这里有现成的,很细致,不再赘述(发音:zhuìshù,敲半天ao'shu,找不到这个词 =.=)   记录一下遇到的问题 1.CocoaPods 版本 ...

  7. spark1.4配置安装

    https://segmentfault.com/a/1190000004508993

  8. 检出商品详情中的图片并替换url

    原有的批量导入是按照系统本身的功能导入商品,现在需要用接口将图片上传图片服务器 所以需要将批量导入的商品图片取出来,上传后替换掉原来的url (1)检出详情中的图片,用文件名做key private ...

  9. 谁应该在CCB(变更控制委员会)中?

    In software development, a Change Control Board (CCB) or Software Change Control Board (SCCB) is a c ...

  10. HTML5 标准规范完成了

    ​    ​万维网联盟(W3C)昨天宣布,HTML5 标准规范终于最终制定完成了,并已公开发布.对于前端工程师来说,这无疑是一个振奋人心的好消息. ​    ​众所周知,HTML5改变了互联网,将成为 ...