实现一个 Trie,包含 insertsearch, 和 startsWith 这三个方法。

注意事项

你可以假设所有的输入都是小写字母a-z。

您在真实的面试中是否遇到过这个题?

Yes
样例

insert("lintcode")
search("code") // return false
startsWith("lint") // return true
startsWith("linterror") // return false
insert("linterror")
search("lintcode) // return true
startsWith("linterror") // return true

思路:这是一道基本的关于字典树的插入,查询和前缀查询的问题。

          本身题目中,插入和查询的过程都非常相似,所以思路相对也很清晰;             

           首先正确定义TrieNode节点类,定义时用一个标记位来标记字符串是否存在。然后定义一个节点类型的指针数组,大小为26,用来存放可能存在的节点。

            插入前先看前缀是否存在。如果存在,就共享,否则创建对应的节点和边。查询过程类似。

/**
* Your Trie object will be instantiated and called as such:
* Trie trie;
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/ /*
思路:这是一道基本的关于字典树的插入,查询和前缀查询的问题。 本身题目中,插入和查询的过程都非常相似,所以思路相对也很清晰; 插入前先看前缀是否存在。如果存在,就共享,否则创建对应的节点和边。
*/ #define MAX_CHILD 26 class TrieNode {
public:
// Initialize your data structure here.
//对于构造函数,需要对对每个节点的指针都进行初始化;
/*
任何指针变量刚被创建时不会自动成为NULL指针,它的值是随机的,它会乱指一气。
所以,指针变量在创建的同时应当被初始化,要么将指针设置为NULL,要么让它指向合法的内存。
*/
int count;
TrieNode* child[MAX_CHILD];
TrieNode() {
for(int i = 0; i < 26; i++)
child[i] = NULL;
count=0; }
}; //在这里,我将每个函数的形参都修改成为了pass-by-reference-const,原因是在effective C++ 中
//建议对于容器使用引用传递可以洁身好多调用拷贝构造函数的时间。 class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(const string& word) {
if(root==NULL||word.size()==0){
return;
} int len=word.size();
TrieNode* t=root;
int i=0; while(i<len){
//从根节点以下开始查找,看前缀是否存在,如果不存在,则插入节点,如果存在则移动指针;
if(t->child[word[i]-'a']==NULL){
TrieNode* temp=new TrieNode();
t->child[word[i]-'a']=temp;
t=t->child[word[i]-'a'];
}
else{
t=t->child[word[i]-'a'];
}
i++;
}
t->count=1;
} // Returns if the word is in the trie.
//可以看到,查询前缀过程和插入过程十分相似;
bool search(const string& word) {
if(root==NULL||word.size()==0){
return false;
} TrieNode* t=root;
int len=word.size();
int i=0; while(i<len){
if(t->child[word[i]-'a']==NULL){
return false;
}
else{
t=t->child[word[i]-'a'];
}
i++;
} if((i==len)&&(t->count==1)){
return true;
} return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(const string& prefix) {
if(root==NULL||prefix.size()==0){
return false;
} int len=prefix.size();
TrieNode* t=root;
int i=0; while(i<len){
if(t->child[prefix[i]-'a']==NULL){
return false;
}
else{
t=t->child[prefix[i]-'a'];
}
i++;
} return true;
} private:
TrieNode* root;
};

Lintcode---实现 Trie的更多相关文章

  1. [LintCode] Implement Trie 实现字典树

    Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...

  2. lintcode 中等题: Implement Trie

    题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例   注意 You may assu ...

  3. Implement Trie(LintCode)

    Implement Trie Implement a trie with insert, search, and startsWith methods. 样例   注意 You may assume ...

  4. [leetcode/lintcode 题解] 微软 面试题:实现 Trie(前缀树)

    实现一个 Trie,包含 ​insert​, ​search​, 和 ​startsWith​ 这三个方法.   在线评测地址:领扣题库官网     样例 1: 输入:    insert(" ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Implement Trie (Prefix Tree) 解答

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

  7. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  8. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  9. lintcode-442-实现 Trie

    442-实现 Trie 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 注意事项 你可以假设所有的输入都是小写字母a-z. 样例 insert(&qu ...

随机推荐

  1. Hibernate 的HQL,QBC 查询语言

    1.HQL:(Hibernate Query Language) 是面向对象的查询语言 1.实体查询 public void testQueryAllDept(){ String hql=" ...

  2. SSL 认证之后,request.getScheme()获取不到https的问题记录

    通过浏览器输入https://www.xxx.com,request.getScheme()获取到的确实http而不是https通过request.getRequestURL()拿到的也是http:/ ...

  3. EF for Oracle 闪退

    EF oracle 加入实体类型时候闪退 主要原因: Oracle.ManagedDataAcces 版本和 SetupODTforVS2015 版本不一致所致. 更新后 SetupODTforVS2 ...

  4. 使用eclipse远程调试weblogic

    配置weblogic远程调试之前,需要做这几个工作:      1) 安装weblogic服务器,然后创建一个域:     2) 安装eclipse集成IDE:     3) eclipse中包含发布 ...

  5. Linux环境下搭建MYSQL数据库指令详情

    一.mysql数据库的安装 确保安装gcc(开发工具) #groupadd mysql #useradd -g mysql mysql #cd /usr/local # tar -zxvf mysql ...

  6. Index 和 Type 的区别

    原文: Index vs. Type By Adrien Grand 译者: fengchang 对于 ES 的新用户来说,有一个常见的问题:要存储一批新的数据时,应该在已有 index 里新建一个 ...

  7. OpenCV 之 霍夫变换

    Hough 变换,对图像中直线的残缺部分.噪声.以及其它的共存结构不敏感,因此,具有很强的鲁棒性. 它常用来检测 直线和曲线 (圆形),识别图像中的几何形状,甚至可用来分割重叠或有部分遮挡的物体. 1 ...

  8. Hibernate开发环境搭建

    一.下载Hibernate包的下载 官网地址:http://hibernate.org/orm/ 下载版本:hibernate-release-4.3.11.Final 二.Hibernate jar ...

  9. 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类

    1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...

  10. C++ String和其他类型互换

    在C++中如何实现String和其他类型互换呢?最好的方式是使用stringstream,下面简单介绍下: 1.其他类型转换为String #include <sstream> strin ...