Implement a trie with insertsearch, and startsWith methods.

实现字典树,前面好像有道题做过类似的东西,代码如下:

 class TrieNode {
public:
// Initialize your data structure here.
TrieNode():isLeaf(false)
{
for(auto & t : dic){
t = NULL;
}
}
TrieNode * dic[];
bool isLeaf;
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode * p = root;
for(int i = ; i < word.size(); ++i){
int index = word[i] - 'a';
if(p->dic[index] == NULL)
p->dic[index] = new TrieNode();
p = p->dic[index];
}
p->isLeaf = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode * p = root;
for(int i = ; i < word.size(); ++i){
int index = word[i] - 'a';
if(p->dic[index] == NULL)
return false;
p = p->dic[index];
}
return p->isLeaf;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode * p = root;
for(int i = ; i < prefix.size(); ++i){
int index = prefix[i] - 'a';
if(p->dic[index] == NULL)
return false;
p = p->dic[index];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))的更多相关文章

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

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

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

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

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

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  4. Java for LeetCode 208 Implement Trie (Prefix Tree)

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

  5. leetcode@ [208] Implement Trie (Prefix Tree)

    Trie 树模板 https://leetcode.com/problems/implement-trie-prefix-tree/ class TrieNode { public: char var ...

  6. LeetCode 208 Implement Trie (Prefix Tree) 字典树(前缀树)

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

  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)

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

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

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

  10. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

随机推荐

  1. Ubuntu下安装keras

    0 系统版本Ubuntu16.04 1 系统更新 sudo apt update sudo apt upgrade 2 安装python基础开发包 sudo apt install -y python ...

  2. maven 介绍(二)

    本文内容主要摘自:http://www.konghao.org/index 内部视频 三.仓库 仓库:本地仓库:远程仓库:私有仓库(nexus) 1. nexus 的安装: 1). 下载并且解压缩 2 ...

  3. QML中的state 状态

    QML中的状态其实很好理解,任何事物在某一事件都是有一个状态的. 比如你看到的一个窗口,这个时候里面的文字和图片正处于某个状态中.比如一个超链接,你点击了,发现颜色变了,你按了Ctrl+A,整个窗体好 ...

  4. Chrome搜索设置

    https://encrypted.google.com/ncr https://encrypted.google.com/search?{google:RLZ}{google:acceptedSug ...

  5. 设置HTML5的video播放速度

    var speed = 1.5;//设置速度 var vdo = document.getElementById("视频的id");//获取id vdo.playbackRate ...

  6. Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation

    1.主要完成的任务是能够将英文转译为法文,使用了一个encoder-decoder模型,在encoder的RNN模型中是将序列转化为一个向量.在decoder中是将向量转化为输出序列,使用encode ...

  7. mysql常见知识点总结

    mysql常见知识点总结 参考: http://www.cnblogs.com/hongfei/archive/2012/10/20/2732516.html https://www.cnblogs. ...

  8. Byte[]分配在哪里?

    http://stackoverflow.com/questions/1113819/arrays-heap-and-stack-and-value-types Your array is alloc ...

  9. codeforce 35C fire again

    2017-08-25 17:04:07 writer:pprp 题目描述: • Codeforces 35C Fire Again• N*M的格子,最开始有K个点 (坐标给定) 开始着火• 每一秒着火 ...

  10. cocos2d-x入门一

    为什么要学cocos2d-x        首先要明白什么是cocos2d-x,能干什么.cocos2d-x是一种跨平台的2D.3D游戏开发工具,目前较为流行的大多数android.ios游戏都是用它 ...