Lintcode---实现 Trie
实现一个 Trie,包含 insert
, search
, 和 startsWith
这三个方法。
注意事项
你可以假设所有的输入都是小写字母a-z。
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的更多相关文章
- [LintCode] Implement Trie 实现字典树
Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...
- lintcode 中等题: Implement Trie
题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assu ...
- Implement Trie(LintCode)
Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assume ...
- [leetcode/lintcode 题解] 微软 面试题:实现 Trie(前缀树)
实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 在线评测地址:领扣题库官网 样例 1: 输入: insert(" ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Implement Trie (Prefix Tree) 解答
Question Implement a trie with insert, search, and startsWith methods. Note:You may assume that all ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode-442-实现 Trie
442-实现 Trie 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 注意事项 你可以假设所有的输入都是小写字母a-z. 样例 insert(&qu ...
随机推荐
- ORADEBUG DOC 12.1.0.2
https://berxblog.blogspot.com/2015/01/oradebug-doc-12102.html this is just an online docu of ORAD ...
- edittext 手机号、邮箱输入限制
package com.example.yanlei.myapplication; import android.support.v7.app.AppCompatActivity;import a ...
- .Net的差评
.NET平台很棒.真的很棒.直到它不再那么棒.我为什么不再用.NET?简单来说,它限制了我们选择的能力(对我来说很重要),转移了我们的注意力,使得我们向内认知它的安全性,替代了帮助我们认知外面广阔世界 ...
- Android内存优化10 内存泄漏常见情况1 静态泄漏
1,内存泄漏到本质是该释放的对象被持久化的对象引用了,造成持久化的常见情况有1,静态持久化 2,线程持久化 线程持久化 因为存活的线程是有dvk虚拟久直接持有,所以存活的线程都是持久化的 内存泄漏1: ...
- 在ios中微信video和audio无法自动播放解决方案
WeixinJSBridgeReady页面初始化的时候会执行 document.addEventListener("WeixinJSBridgeReady", function ( ...
- 逻辑回归Logistic Regression 之基础知识准备
0. 前言 这学期 Pattern Recognition 课程的 project 之一是手写数字识别,之二是做一个网站验证码的识别(鸭梨不小哇).面包要一口一口吃,先尝试把模式识别的经典问题—— ...
- OTL调用存储过程/函数及注意事项
OTL 是 Oracle, Odbc and DB2-CLI Template Library 的缩写,是一个 C++ 编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库. OTL ...
- DBA数据库信息查询常用SQL
常用DBA脚本1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size from dba_tabl ...
- 远程访问ubuntu下mysql的问题
ubuntu下mysql不能用IP地址远程访问的问题解决 方法1: 1.mysql>grant all privileges on *.* to 'root'@'%' identified by ...
- 一起talk GDB吧(第五回:GDB查看信息)
各位看官们.大家好,上一回中我们说的是GDB的调用栈调试功能,而且说了怎样使用GDB进行查看调用 栈.这一回中,我们继续介绍GDB的调试功能:查看信息.当然了.我们也会介绍怎样使用GDB查看程序 执行 ...