Trie树[前缀树,字典树]
来源:互联网 发布:网络发票查询 编辑:程序博客网 时间:2024/06/07 23:39
Implement a trie with insert
, search
, and startsWith
methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z
.
Subscribe to see which companies asked this question
class TrieNode {public: // Initialize your data structure here. TrieNode() { memset(child,NULL,sizeof(child)); flag=false; } TrieNode* child[26]; bool flag;};class Trie {public: Trie() { root = new TrieNode(); } // Inserts a word into the trie. void insert(string word) { TrieNode* p=root; for(int i=0;i<word.size();i++) { if(p->child[word[i]-'a']!=NULL) { p=p->child[word[i]-'a']; } else { p->child[word[i]-'a']=new TrieNode(); p=p->child[word[i]-'a']; } } p->flag=true; //表明到此处为一个字符串 } // Returns if the word is in the trie. bool search(string word) { TrieNode* p=root; for(int i=0;i<word.size();i++) { if(p->child[word[i]-'a']==NULL) return false; p=p->child[word[i]-'a']; } if(p->flag) return true; return false; } // 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=0;i<prefix.size();i++) { if(p->child[prefix[i]-'a']==NULL) return false; p=p->child[prefix[i]-'a']; } return true; }private: TrieNode* root;};// Your Trie object will be instantiated and called as such:// Trie trie;// trie.insert("somestring");// trie.search("key");
0 0
- Trie树(前缀树,字典树)
- Trie树,字典树,前缀树
- Trie树(字典树、前缀树)
- Trie树[前缀树,字典树]
- 前缀树 / 字典树 / Trie树
- Trie(prefix tree,前缀树,字典树)
- Trie(前缀树,字典树)
- Trie树trie(前缀)
- 字典树(Trie树、单词查找树、前缀树)
- Hash树(散列树)和Trie树(字典树、前缀树)
- Trie(数字树、字典树、前缀树)
- Trie树(字典树,前缀树,键树)分析详解
- 常用算法之Trie【字典树,前缀树】
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
- LeetCode Implement Trie (Prefix Tree) 字典树/前缀树
- 【模板】【字符串】Trie(前缀树、字典树)
- 前缀树(字典树)Trie的c代码
- 字符串前缀:字典树(Trie)的应用
- 数据库 sql 回顾2
- sklearn SVR与KRR对比
- hdu 1181 变形课(DFS)
- The Frog's Games(二分)
- 用windows自带功能轻松建立自己的FTP网站
- Trie树[前缀树,字典树]
- Oracle 11g R2 DBA 操作指南(7)
- POJ 3170--双重BFS
- 大数据hadoop资源网址
- Android签名机制之---签名过程详解
- 【整理】div与span的区别
- MultiPath TCP - Users - Manual
- 栈
- 生活、工作点滴(一)