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. 64位python安装MySQL-python 1.2.5

    在64位的python直接安装MySQL-python 1.2.5有问题,参考http://www.linuxfly.org/windows_install_mysql_python_library/ ...

  2. Dom之标签增删操作

    dom操作:THML新增子标签 a标签(appendChild) <!DOCTYPE html><html lang="en"><head> & ...

  3. ubuntu 安装 boost

    1.tar -zxvf  boost_1_43_0.tar.gz 2.cd boost_1_43_0, 执行: sudo ./bootstrap.sh sudo ./bjam install 检验安装 ...

  4. eclipse开发Android程序sdk和avd的图标不见了

    在eclipse中开发我们的Android程序时,安装sdk是必不可少的,有时候会出现sdk和avd的图标都不见了的情况,一般出现这种情况的原因是你从别处copy了一个sdk的包然后直接在引用造成的, ...

  5. jquery淡入淡出

    html代码: <button id="b1" type="button">淡出</button> <button id=&quo ...

  6. Spring security与shiro

    shiro更轻量级,spring security过于复杂. Apache Shiro 使用手册(一)Shiro架构介绍 Spring Security笔记:Remember Me(下次自动登录)

  7. Neutron LBaaS Service(2)—— Neutron Services Insertion Model

    Service Insertion Service Insertion是Neutron中实现L4/L7层服务的框架.Neutron以前只有一级插件结构用于实现各种L2层技术(如LinuxBridge, ...

  8. lucene之排序、设置权重、优化、分布式搜索(转)

    lucene之排序.设置权重.优化.分布式搜索(转) 1. 基本应用 using System;using System.Collections.Generic;using System.Text;u ...

  9. bzojj1764: [Baltic2009]monument

    Description 给一个p*q*r的立方体,它由p*q*r个1*1*1的小立方体构成.每个立方体要么被虫蛀,要么不被.现在郑爽要选出一个a*a*b的立方体(方向任意),使得它没有被虫蛀过,并且4 ...

  10. linux脚本后台运行

    一般情况下,linux运行脚本是随着终端的关闭而关闭的,那么怎么让脚本能够在后台运行并且不随终端关闭而关闭呢? 这时用到的是nohup命令 格式: nohup 脚本路径 & 例: nohup ...