Implement a trie with insertsearch, 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)的更多相关文章

  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. LeetCode——Implement Trie (Prefix Tree)

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

  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. 程序员书单_java专项进阶篇

    JDBC API数据库编程实作教材 http://download.csdn.net/detail/shenzhq1980/9145715 Java事务设计模式 http://download.csd ...

  2. 【linux】linux如何进入单人维护模式修改root密码

  3. MySQL加载并执行SQL脚本文件

    第一种方法: 命令行下(未连接数据库) ,输入 mysql -h localhost -u root -p123456 < C:\db.sql 第二种方法: 命令行下(已连接数据库,此时的提示符 ...

  4. bzoj1803: Spoj1487 Query on a tree III

    Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the ...

  5. php定时执行脚本

    php定时执行脚本 ignore_user_abort(); // run script. in background set_time_limit(0); // run script. foreve ...

  6. 使用express搭建第一个Web应用【Node.js初学】

    来源:http://jingyan.baidu.com/article/bad08e1ee501e009c8512106.html express是一个开源的node.js项目框架,初学者使用expr ...

  7. 【MySQL】性能优化 之 延迟关联

    [背景]  某业务数据库load 报警异常,cpu usr 达到30-40 ,居高不下.使用工具查看数据库正在执行的sql ,排在前面的大部分是: SELECT id, cu_id, name, in ...

  8. Linux系统默认服务建议开启关闭说明列表

    服务名称 功能简介 建议 acpid 电源管理接口.如果是笔记本用户建议开启,可以监听内核层的相关电源事件. 开启 anacron 系统的定时任务程序.cron的一个子系统,如果定时任务错过了执行时间 ...

  9. 黄聪:C#禁止Webbrowser中的脚本错误提示,自动屏蔽弹出窗口

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...

  10. jQuery插件:用于获取元素自身的HTML内容

    jQuery.fn.outerHTML = function(s) { return (s) ? this.before(s).remove() : $("<Hill_man>& ...