leetCode_字典树
来源:互联网 发布:ubuntu.com 编辑:程序博客网 时间:2024/06/06 18:08
好吧,又写了一遍字典树,已经写了n遍了。不过希望下次把线段树学会~
上代码:
class TrieNode {public: int tag; TrieNode *next[26]; // Initialize your data structure here. TrieNode() { int i; for(i=0;i<26;i++) next[i]=NULL; tag=0; }};class Trie {public: Trie() { root = new TrieNode(); } // Inserts a word into the trie. void insert(string word) { TrieNode *tempRoot=root; int i; for(i=0;i<word.length();i++) { if(tempRoot->next[word[i]-'a']==NULL) { TrieNode * newNode=new TrieNode(); tempRoot->next[word[i]-'a']=newNode; } tempRoot=tempRoot->next[word[i]-'a']; } tempRoot->tag=1; } // Returns if the word is in the trie. bool search(string word) { TrieNode *tempRoot=root; int i; for(i=0;i<word.length();i++) { if(tempRoot->next[word[i]-'a']!=NULL) tempRoot=tempRoot->next[word[i]-'a']; else return false; } return tempRoot->tag==1; } // Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string word) { TrieNode *tempRoot=root; int i; for(i=0;i<word.length();i++) { if(tempRoot->next[word[i]-'a']!=NULL) tempRoot=tempRoot->next[word[i]-'a']; else return false; } return true; }private: TrieNode* root;};
0 0
- leetCode_字典树
- leetcode_树_相关内容
- leetcode_链表转成二叉搜索树
- leetCode_线段树、单点更新、区间求和
- LeetCode_将LeetCode中的字符串用例转换成二叉树
- leetcode_解题_reorder list
- leetcode_动态规划
- leetcode_题解_Surrounded Regions
- leetcode_题解_Clone Graph
- leetcode_题解_min stack
- leetcode_题解_path sum_easy
- **(leetcode_ string) ZigZag Conversion
- ***(leetcode_ backtracking) Subsets
- LeetCode_个人题解
- LeetCode_ Invert Binary Tree
- LeetCode_ Invert Binary Tree
- leetcode_ Bulls and Cows
- leetCode_ Surrounded Regions(BFS)
- MySQL用变量的方法添加伪序号列
- 内联函数
- Android二维码ZXing开源框架的学习总结
- Postgresql Dblink相关函数
- arm linux 移植usb转串口驱动
- leetCode_字典树
- Android进程间通信之Socket
- Oracle - SQL学习笔记 1
- 自动取款机ATM取款系统 课题项目
- NYOJ 151 Biorhythms --剩余定理
- Scala入门到精通——第十四节 Case Class与模式匹配(一),scala入门到精通
- 换个思维透透气
- 一颗核桃的逆袭之路
- 【HDU】1087 - Super Jumping! Jumping! Jumping!(dp)